在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:29:48  来源:igfitidea点击:

Library to query HTML with XPath in Java?

javahtmlxhtmlxpath

提问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

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

You could use TagSouptogether with Saxon. That way you simply replace any XML SAX parser used with TagSoup and the XPath 2.0 or XSLT 2.0 or XQuery 1.0 implementation works as usual.

您可以将TagSoup与 Saxon 一起使用。这样,您只需替换与 TagSoup 一起使用的任何 XML SAX 解析器,XPath 2.0 或 XSLT 2.0 或 XQuery 1.0 实现就可以照常工作。

回答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 结合使用。