如何用Java高效地解析HTML?

发布于 2024-08-20 04:57:30 字数 362 浏览 7 评论 0原文

我在工作中进行了大量的 HTML 解析。到目前为止,我一直在使用 HtmlUnit 无头浏览器进行解析和浏览器自动化。

现在,我想将这两个任务分开。

我想使用轻量级 HTML 解析器,因为在 HTMLUnit 中首先加载页面,然后获取源代码,然后解析它需要花费很多时间。

我想知道哪个 HTML 解析器可以有效地解析 HTML。我需要

  1. Speed
  2. Ease 通过“id”或“名称”或“标签类型”来定位任何 HtmlElement。

如果它不清除脏的 HTML 代码,那对我来说就没问题了。我不需要清理任何 HTML 源代码。我只需要最简单的方法来移动 HtmlElements 并从中获取数据。

I do a lot of HTML parsing in my line of work. Up until now, I was using the HtmlUnit headless browser for parsing and browser automation.

Now, I want to separate both tasks.

I want to use a light HTML parser because it takes much time in HTMLUnit to first load a page, then get the source, and then parse it.

I want to know which HTML parser can parse HTML efficiently. I need

  1. Speed
  2. Ease to locate any HtmlElement by its "id" or "name" or "tag type".

It would be ok for me if it doesn't clean the dirty HTML code. I don't need to clean any HTML source. I just need the easiest way to move across HtmlElements and harvest data from them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

征﹌骨岁月お 2024-08-27 04:57:30

jsoup

自插件:我刚刚发布了一个新的 Java HTML 解析器:jsoup。我在这里提到它是因为我认为它会做你所追求的事情。

它的技巧是使用 CSS 选择器语法来查找元素,例如:

String html = "<html><head><title>First parse</title></head>"
  + "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
Elements links = doc.select("a");
Element head = doc.select("head").first();

请参阅 Selector< /a> javadoc 了解更多信息。

这是一个新项目,因此非常欢迎任何改进的想法!

jsoup

Self plug: I have just released a new Java HTML parser: jsoup. I mention it here because I think it will do what you are after.

Its party trick is a CSS selector syntax to find elements, e.g.:

String html = "<html><head><title>First parse</title></head>"
  + "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
Elements links = doc.select("a");
Element head = doc.select("head").first();

See the Selector javadoc for more info.

This is a new project, so any ideas for improvement are very welcome!

夏末的微笑 2024-08-27 04:57:30

到目前为止我见过的最好的是 HtmlCleaner

HtmlCleaner 是用 Java 编写的开源 HTML 解析器。 Web 上的 HTML 通常是脏的、格式错误的并且不适合进一步处理。对于此类文档的任何严重消耗,有必要首先清理混乱并为标签、属性和普通文本带来顺序。对于给定的 HTML 文档,HtmlCleaner 会对各个元素重新排序并生成格式良好的 XML。默认情况下,它遵循大多数 Web 浏览器用于创建文档对象模型的类似规则。但是,用户可以提供自定义标签和规则集以进行标签过滤和平衡。

借助 HtmlCleaner,您可以使用 XPath 定位任何元素。

对于其他 html 解析器,请参阅这个问题

The best I've seen so far is HtmlCleaner:

HtmlCleaner is open-source HTML parser written in Java. HTML found on Web is usually dirty, ill-formed and unsuitable for further processing. For any serious consumption of such documents, it is necessary to first clean up the mess and bring the order to tags, attributes and ordinary text. For the given HTML document, HtmlCleaner reorders individual elements and produces well-formed XML. By default, it follows similar rules that the most of web browsers use in order to create Document Object Model. However, user may provide custom tag and rule set for tag filtering and balancing.

With HtmlCleaner you can locate any element using XPath.

For other html parsers see this SO question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文