如何用Java有效地解析HTML?

发布于 2025-01-31 08:29:48 字数 327 浏览 3 评论 0 原文

我在工作中进行了很多HTML解析。到目前为止,我一直在使用HTMLUNIT无头浏览器来解析和浏览器自动化。

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

我想使用轻型HTML解析器,因为在HTMLUNIT中需要大量时间才能首先加载页面,然后获取源,然后将其解析。

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

  1. 易于速度
  2. 来通过其“ ID”或“名称”或“标签类型”来找到任何HTMLELEMENT。

如果不清洁脏HTML代码,对我来说是可以的。我不需要清洁任何HTML源。我只需要最简单的方法来跨越HTMLELEMENT并从中收集数据。

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

指尖凝香 2025-02-07 08:29:48

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 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!

我家小可爱 2025-02-07 08:29:48

到目前为止,我看到的最好的是 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.

追我者格杀勿论 2025-02-07 08:29:48

我建议 validator.nu的解析器,基于HTML5解析算法。 是Mozilla在2010-05-03 中使用的Parser

I suggest Validator.nu's parser, based on the HTML5 parsing algorithm. It is the parser used in Mozilla from 2010-05-03

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