Python网络爬虫笔记(一):网页抓取方式和LXML示例
2020-02-02 17:11
691 查看
(一) 三种网页抓取方法
1、 正则表达式:
模块使用C语言编写,速度快,但是很脆弱,可能网页更新后就不能用了。
2、 Beautiful Soup
模块使用Python编写,速度慢。
安装:
pip install beautifulsoup4
3、 Lxml
模块使用C语言编写,即快速又健壮,通常应该是最好的选择。
(二) Lxml安装
pip install lxml
如果使用lxml的css选择器,还要安装下面的模块
pip install cssselect
(三) 使用lxml示例
1 import urllib.request as re 2 import lxml.html 3 #下载网页并返回HTML 4 def download(url,user_agent='Socrates',num=2): 5 print('下载:'+url) 6 #设置用户代理 7 headers = {'user_agent':user_agent} 8 request = re.Request(url,headers=headers) 9 try: 10 #下载网页 11 html = re.urlopen(request).read() 12 except re.URLError as e: 13 print('下载失败'+e.reason) 14 html=None 15 if num>0: 16 #遇到5XX错误时,递归调用自身重试下载,最多重复2次 17 if hasattr(e,'code') and 500<=e.code<600: 18 return download(url,num=num-1) 19 return html 20 html = download('https://tieba.baidu.com/p/5475267611') 21 #将HTML解析为统一的格式 22 tree = lxml.html.fromstring(html) 23 # img = tree.cssselect('img.BDE_Image') 24 #通过lxml的xpath获取src属性的值,返回一个列表 25 img = tree.xpath('//img[@class="BDE_Image"]/@src') 26 x= 0 27 #迭代列表img,将图片保存在当前目录下 28 for i in img: 29 re.urlretrieve(i,'%s.jpg'%x) 30 x += 1
转载于:https://www.cnblogs.com/simple-free/p/8757758.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 按示例学python:使用python抓取网页正文
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成
- scala 学习笔记(02) 元组Tuple、数组Array、Map、文件读写、网页抓取示例
- 转载:[Python]网络爬虫(一):抓取网页的含义和URL基本构成
- python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容
- 用python的curl和lxml来抓取和分析网页内容
- Python网页抓取之Lxml
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- Python爬虫系列之----Scrapy(五)网页提取的三种方式(正则,Beautiful Soup,Lxml)
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成
- 鱼c笔记——Python爬虫(一):利用urllib进行简单的网页抓取
- python网络爬虫-爬取网页的三种方式(1)
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- Python使用正则表达式抓取网页图片的方法示例
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容