Java 扫描器查找标签,然后分隔符将该标签中的内容写入文件
Java Scanner to find a tag, then delimiters to write what's in that tag to a file
我正在编写一个程序,用于搜索网站的 HTML,找到特定标签,然后将该标签的内容写入文件。例如,HTML 可能如下所示:
<div class="something" specific-tag:"print this 1">some content</div>
<div class="something" not-the-right-tag:"don't print this">some content</div>
<div class="something" specific-tag:"print this 2">some content</div>
<div class="something" not-the-right-tag:"don't print this">some content</div>
<div class="something" specific-tag:"print this 3">some content</div>
所需的文件输出如下所示:
print this 1
print this 2
print this 3
我知道如何使用扫描器 class 来查找特定标签,在本例中 "specific-tag" 我知道如何使用分隔符写入文件,在本例中分隔符是 ",但我不知道该怎么做是搜索一个标签,然后将该标签后分隔符之间的所有内容写入文件,然后继续搜索下一个标签并重复直到文件末尾。
想法?
您确实应该使用某种 html 解析库。快速 google 搜索显示了这个 http://jsoup.org/。它似乎易于使用。打电话
Elements divs = doc.select("div[specific-tag]");
应该生成 div,然后您可以提取特定标签属性。
我正在编写一个程序,用于搜索网站的 HTML,找到特定标签,然后将该标签的内容写入文件。例如,HTML 可能如下所示:
<div class="something" specific-tag:"print this 1">some content</div>
<div class="something" not-the-right-tag:"don't print this">some content</div>
<div class="something" specific-tag:"print this 2">some content</div>
<div class="something" not-the-right-tag:"don't print this">some content</div>
<div class="something" specific-tag:"print this 3">some content</div>
所需的文件输出如下所示:
print this 1
print this 2
print this 3
我知道如何使用扫描器 class 来查找特定标签,在本例中 "specific-tag" 我知道如何使用分隔符写入文件,在本例中分隔符是 ",但我不知道该怎么做是搜索一个标签,然后将该标签后分隔符之间的所有内容写入文件,然后继续搜索下一个标签并重复直到文件末尾。
想法?
您确实应该使用某种 html 解析库。快速 google 搜索显示了这个 http://jsoup.org/。它似乎易于使用。打电话
Elements divs = doc.select("div[specific-tag]");
应该生成 div,然后您可以提取特定标签属性。