python - 'BeautifulSoup' 和 'lxml' 之间有什么关系?

标签 python beautifulsoup html-parsing lxml

lxmldoc 中,它说:

lxml can interface to the parsing capabilities of BeautifulSoup through the lxml.html.soupparser module. It provides three main functions: fromstring() and parse() to parse a string or file using BeautifulSoup into an lxml.html document, and convert_tree() to convert an existing BeautifulSoup tree into a list of top-level Elements.

同时,BS'也可以使用lxml作为解析器。 [ref]

Beautiful Soup supports the HTML parser included in Python’s standard library, but it also supports a number of third-party Python parsers. One is the lxml parser.

BS建议 使用 lxml 作为速度解析器。

如果 lxml 使用 BS 进行解析,而 BS 的解析器反过来是 lxml 呢?

我一直绞尽脑汁想弄清楚他们的关系。帮助。

最佳答案

BS 解析器和lxml.html 解析器应该没有什么混淆的地方。 BS 有一个 HTML 解析器,而 lxml 有它自己的 HTML 解析器。

BS 您引用的文档只是说您可以使用 lxml 解析器或其他可能的第三方解析器将 HTML 解析为 BS soup 对象,作为使用默认 BS 解析器的替代方法:

BeautifulSoup(markup, "lxml")

同样,lxml 文档说您可以使用 BS 解析器将 HTML 解析为 lxml 树对象,作为使用默认 lxml.html 解析器:

root = lxml.html.soupparser.fromstring(tag_soup)

关于python - 'BeautifulSoup' 和 'lxml' 之间有什么关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32198565/

相关文章:

python - `is` 运算符在处理非缓存整数时出现意外行为

python - Matplotlib 保存为 pdf 未在条形图中显示阴影线 - 潜在错误?

python - 无法放置 ModelViewSet Django Rest Framework

python - 如何使用 beautifulsoup python 使用 findall 指定子标签

html - Beautiful Soup Prettify(formatter = 'xml') 已被弃用?

Java:我有一大串 html,需要提取 href ="..."文本

python - 为 Pika ioloop 异步设置超时 (RabbitMQ)

java - 使用 Process Builder 从 Java 运行 python

xml-parsing - 创建 `Nokogiri::XML` 或 `Nokogiri::HTML` 对象时如何避免创建不重要的空白文本节点

python - 从页面获取特定图像