在 Java 中使用 XPath 查询 HTML 的库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3361263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Library to query HTML with XPath in Java?
提问by Leonardo Marques
Can anyone recommend me a java library to allow me XPath Queries over URLs? I've tried JAXP without success.
任何人都可以向我推荐一个允许我通过 URL 进行 XPath 查询的 Java 库吗?我试过 JAXP 没有成功。
Thank you.
谢谢你。
回答by Mark Butler
There are several different approaches to this documented on the Web:
Web 上记录了几种不同的方法:
Using HtmlCleaner
- HtmlCleaner / Java DOM parser - Using XPath Contains against HTML in Java(This is the way I recommend)
- HtmlCleaner itself has a built in utility supporting XPath - See the javadocs http://htmlcleaner.sourceforge.net/doc/org/htmlcleaner/XPather.htmlor this example http://thinkandroid.wordpress.com/2010/01/05/using-xpath-and-html-cleaner-to-parse-html-xml/
- HtmlCleaner / Java DOM parser -在 Java 中对 HTML 使用 XPath 包含(这是我推荐的方式)
- HtmlCleaner 本身具有支持 XPath 的内置实用程序 - 请参阅 javadocs http://htmlcleaner.sourceforge.net/doc/org/htmlcleaner/XPather.html或此示例http://thinkandroid.wordpress.com/2010/01/05 /using-xpath-and-html-cleaner-to-parse-html-xml/
Using Jericho
使用耶利哥
I have tried a few different variations of these approaches, i.e. HtmlParser plus the Java DOM parser, and JSoup plus Jaxen, but the combination that worked best is HtmlCleaner plus the Java DOM parser. The next best combination was Jericho plus Jaxen.
我尝试了这些方法的几种不同变体,即 HtmlParser 加 Java DOM 解析器,以及 JSoup 加 Jaxen,但效果最好的组合是 HtmlCleaner 加 Java DOM 解析器。次佳组合是 Jericho 和 Jaxen。
回答by Artem Barger
jsoup, Java HTML ParserVery similar to jQuery syntax way.
jsoup,Java HTML Parser非常类似于jQuery 的语法方式。
回答by Martin Honnen
回答by Tassos Bassoukos
I've used JTidy to make HTML into a proper DOM, then used plain XPath to query the DOM.
我已经使用 JTidy 将 HTML 变成了一个合适的 DOM,然后使用普通的 XPath 来查询 DOM。
If you want to do cross-document/cross-URL queries, better use JTidy with XQuery.
如果您想进行跨文档/跨 URL 查询,最好将 JTidy 与 XQuery 结合使用。

