BeautifulSoup中各种html解析器的比較及使用

Beautiful Soup解析器比較

·Beautiful Soup支持各种html解析器。包含python自带的标准库。还有其它的很多第三方库模块。

当中一个就是lxml parser,至于lxml parser的安装,能够通过下面方法安装:

1)easy_install lxml   2)pip install lxml    

另外。python对于模块的安装,能够查看博客说明。分为两种:easy_install pip.

第二种纯python解析器为html5lib解析器。能够像web浏览器那样解析html页面,你能够通过以下两种方式安装html5lib

1)easy_install html5lib   2)pip install html5lib

以下对各种html解析器的优缺点做一下对照:


解析器 用法 长处 缺点
Python’s html.parser BeautifulSoup(markup,"html.parser")
  • python自身带有
  • 速度比較快
  • 能较好兼容 (as of Python 2.7.3 and 3.2.)
不能非常好地兼容(before Python 2.7.3 or 3.2.2)
lxml’s HTML parser BeautifulSoup(markup,"lxml")
  • 速度非常快
  • 兼容性好
External C dependency
lxml’s XML parser BeautifulSoup(markup, "lxml-xml") BeautifulSoup(markup,"xml")    速度非常快
  • The only currently supported XML parser
External C dependency
html5lib BeautifulSoup(markup, "html5lib") 1)兼容性非常好
2)能够像web浏览器一样解析html页面
3) Creates valid HTML5
  • 速度非常慢
  • External Python dependency

假设你想追求速度的话。建议使用lxml,假设你使用的python版本号2.x是2.7.3之前的版本号,或者python3.x的是3.2.2之前的版本号。你非常有必要安装使用html5lib或lxml使用。由于python内建的html解析器不能非常好地适应于这些老版本号。



原文地址:https://www.cnblogs.com/wzzkaifa/p/7111431.html