我正在编写一个程序,该程序旨在搜索网站的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
我知道如何使用Scanner类查找特定标签,在这种情况下为“ specific-tag”,并且我知道如何使用定界符将文件写入文件,在这种情况下,定界符为“,但是我不知道如何要做的就是搜索标签,然后在该标签后的定界符之间的所有内容都写入文件,然后继续搜索下一个标签并重复直到文件结束。
有什么想法吗?
您确实应该使用某种html解析库。谷歌快速搜索显示了该http://jsoup.org/。似乎很容易使用。呼唤
Elements divs = doc.select("div[specific-tag]");
应该产生div,然后您可以提取specific-tag属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句