爬取网页后的抓取数据_3种抓取网页数据方法
发布时间
阅读量:
阅读量
1. 正则表达式
(1)
re.findall('<tr id="places_area__row">.*?<td\s*class=["\']w2p_fw["\']>(.*?)</td>', html)
代码解读
(2)
import re
pattern = re.compile("hello")
#match_list = re.findall(pattern, "hello world! hello") 这个是找全部匹配的,返回列表
match = pattern.match("hello world! hello") #这个是找匹配的,有就返回一个,没有返回None
print(match)
代码解读
2. BeautifulSoup(bs4)
学习Python中掌握使用Beautiful Soup库的非常详细教程:http://www.jb51.net/article/65287.htm
from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html, "html.parser") #用html解释器对得到的html文本进行解析
>>> tr = soup.find(attrs={"id":"places_area__row"})
>>> tr
<tr id="places_area__row"><td class="w2p_fl"><label for="places_area" id="places_area__label">Area: </label></td><td class="w2p_fw">244,820 square kilometres</td><td class="w2p_fc"></td></tr>
>>> td = tr.find(attrs={"class":"w2p_fw"})
>>> td
<td class="w2p_fw">244,820 square kilometres</td>
>>> area = td.text
>>> print(area)
244,820 square kilometres
代码解读
3. Lxml
Lxml是以libxml2这一XML解析库为基础开发而成的Python封装模块。该模块采用C语言进行编写,在性能上显著优于BeautifulSoup工具。通过书中对比分析得出的结论可知,在爬取网页并进行数据采集的过程中,默认采用以下步骤:首先对网页源码进行解析(采用上述三种工具中的Lxml方法),随后选择所需数据(采用CSS选择器进行定位)。
#先解析网页源码(lxml)示例
import lxml.html
broken_html = "<ul class=country><li>Area<li>Population</ul>"
tree = lxml.html.fromstring(broken_html) #解析已经完成
fixed_html = lxml.html.tostring(tree, pretty_print=True)
print(fixed_html)
#output
#b'<ul class="country">\n<li>Area</li>\n<li>Population</li>\n</ul>\n'
代码解读
#解析网页源码(lxml)后使用css选择器提取目标信息
import lxml.html
import cssselect
html = download("http://example.webscraping.com/view/Aland-Islands-2") #下载网页
html = str(html)
tree = lxml.html.fromstring(html) #解析已经完成
td = tree.cssselect("tr#places_area__row > td.w2p_fw")[0] #选择id="plac..."名为tr的标签下的,class="w2p..."名为td的标签中[0]元素
area = td.text_content() #目标信息area值为td标签中的text信息
print(area)
代码解读

以上三种方法性能对比与结论:

全部评论 (0)
还没有任何评论哟~
