且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

美丽的汤和餐桌刮面-LXML与HTML解析器

更新时间:2021-12-18 04:01:38

BeautifulSoup文档中有一个特殊段落,称为

There is a special paragraph in BeautifulSoup documentation called Differences between parsers, it states that:

Beautiful Soup将相同的界面呈现给许多不同的人 解析器,但是每个解析器都是不同的.不同的解析器将创建 同一文档中的不同解析树.最大的不同 在HTML解析器和XML解析器之间.

Beautiful Soup presents the same interface to a number of different parsers, but each parser is different. Different parsers will create different parse trees from the same document. The biggest differences are between the HTML parsers and the XML parsers.

在格式不正确的HTML文档中,差异显而易见.

The differences become clear on non well-formed HTML documents.

其道理是,您应该使用在特定情况下有效的解析器.

The moral is just that you should use the parser that works in your particular case.

还请注意,您应该始终明确指定要使用的解析器.这将帮助您避免在不同的计算机或虚拟环境上运行代码时出现意外.

Also note that you should always explicitly specify which parser are you using. This would help you to avoid surprises when running the code on different machines or virtual environments.