c libxml2解析html,在Python中,lxml和libxml2哪个更适合解析格式错误的html?

在libxml2 page中,您可以看到以下注释:Note that some of the Python purist dislike the default set of Python bindings, rather than complaining I suggest they have a look at lxml the more pythonic bindings for libxml2 and libxslt and check the mailing-list.

在lxml页面中,另一个:The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt. It is unique in that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API, mostly compatible but superior to the well-known ElementTree API.

所以本质上,使用lxml你得到了完全相同的功能,

但是使用一个与标准库中的ElementTree库兼容的pythonicapi(所以这意味着标准库文档将有助于学习如何使用lxml)。这就是为什么,lxml比libxml2更受欢迎(即使底层实现是相同的)。在

编辑:正如其他答案所解释的,要解析格式错误的html,最好的选择是使用^{}。值得注意的一点是,如果您安装了lxml,BeautifulSoup将按照新版本的documentation中的说明使用它:If you don’t specify anything, you’ll get the best HTML parser that’s installed. Beautiful Soup ranks lxml’s parser as being the best, then html5lib’s, then Python’s built-in parser.

无论如何,即使BeautifulSoup在幕后使用了lxml,您也可以直接解析无法用xml解析的损坏的{}。例如:>>> lxml.etree.fromstring('')

...

XMLSyntaxError: Premature end of data in tag html line 1, line 1, column 7

但是:

^{pr2}$

最后,请注意lxml还提供了一个到{}旧版本的接口,如下所示:>>> lxml.html.soupparser.fromstring('')

所以在一天结束时,您可能会使用lxml和{}。你唯一要选择的就是你最喜欢的API是什么。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值