html数据抽取方法对比

Python中常用的html数据抽取方法有正则、XPath和BeautifulSoup这三种。其中,最常用的XPath库是lxml。今天再介绍一个库SimplifiedDoc,一起比较一下他们的优劣。
1、安装

名称安装方法包大小说明
正则不需安装(内置)
lxmlpip install lxml4.5MB依赖c语言库
BeautifulSouppip install beautifulsoup4107kB如果不使用第三方库,则不需要别的安装
SimplifiedDocpip install simplified-scrapy43kB没有第三方依赖

2、Python版本支持
这几种方法都同时支持Python2和Python3。

3、使用方法
对正则和XPath的使用方法,这里就不重复了,只简单对比下BeautifulSoup和SimplifiedDoc。下面的代码展示了两者实例化及提取数据的方法。

html = '''<html> <head> <title>Example Domain</title> </head> <body> <div id='test'> test text </div> </body></html>'''# 例子:http://www.jsphp.net/python/show-24-214-1.htmlfrom bs4 import BeautifulSoupsoup = BeautifulSoup(html,features='html.parser')soup = BeautifulSoup(html,features='lxml')title = soup.title# 取所有divs = soup.findAll(id='test')divs = soup.select('div#test')# 取第一个div = soup.find(id='test')div = soup.select_one('div#test')print (div.text)# 例子:https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examplesfrom simplified_scrapy import SimplifiedDocdoc = SimplifiedDoc(html)title = doc.title# 取所有divs = doc.getElements('div',attr='id',value='test')divs = doc.selects('div#test')# 取第一个div = doc.getElement('div',attr='id',value='test')div = doc.select('div#test')print (div.text)

在使用方法上,有相似的地方,也有不同的地方,但是都挺简单的。这里特别提一下SimplifiedDoc中的getElement方法,每个方法中都有三个可选的参数start=None,end=None,before=None。使用这三个参数,可以帮助定位需要抽取的数据,在合适的时候,可以使抽取很方便。

4、性能对比
在处理速度上,对于正则,处理速度快,并且是有针对性的只处理需要的数据,所以比较公认的是处理速度最快的方式,但是使用起来相对困难。下面只对比lxml、BeautifulSoup、SimplifiedDoc这三种方式。对比代码如下:

from lxml import etreefrom bs4 import BeautifulSoupfrom simplified_scrapy import SimplifiedDocimport timehtml = '''<!doctype html><html><head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style></head><body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div></body></html>'''start = time.time()for i in range(0,1000): root = etree.HTML(html) text = root.xpath('//h1/text()')[0]print (time.time()-start,text)start = time.time()for i in range(0,1000): soup = BeautifulSoup(html,features='html.parser') text = soup.h1.textprint (time.time()-start,text)start = time.time()for i in range(0,1000): soup = BeautifulSoup(html,features='lxml') text = soup.h1.textprint (time.time()-start,text)start = time.time()for i in range(0,1000): doc = SimplifiedDoc(html) text = doc.h1.htmlprint (time.time()-start,text)

使用VSCode测试对比结果如下:

名称调试模式耗时(单位:秒)
lxml0.10795402526855469
BeautifulSoup(html.parser)2.5450849533081055
BeautifulSoup(lxml)2.236968994140625
SimplifiedDoc0.25988101959228516
名称非调试模式耗时(单位:秒)
lxml0.12264490127563477
BeautifulSoup(html.parser)0.799994945526123
BeautifulSoup(lxml)0.7144896984100342
SimplifiedDoc0.14832687377929688

不管调试模式或非调试模式,lxml的速度是最快的,SimplifiedDoc第二,BeautifulSoup第三。其中有一个奇怪的地方不知道是怎么回事,非调试模式下较调试模式下速度都相对提高,lxml却是变慢了。
5、总结
lxml不负众望速度是除正则外最快的,小众的SimplifiedDoc速度挺快,使用方法也简单,值得大家试用一下。

名称安装难度使用难度速度包大小
正则无(内置)困难最快
lxml一般一般
BeautifulSoup简单容易较小
SimplifiedDoc简单容易较快

相关文章