python爬虫数据-下载图片经典案例
2020-01-15 10:39
639 查看
'''
Urllib 模块提供了读取web页面数据的接口,我们可以像读取本地文件一样读取www和ftp上的数据。首先,我们定义了一个getHtml()函数:
urllib.urlopen()方法用于打开一个URL地址。
read()方法用于读取URL上的数据,向getHtml()函数传递一个网址,并把整个页面下载下来。执行程序就会把整个网页打印输出。
'''
# 筛选页面中想要的数据
import re
import urllib.request
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
html = html.decode('utf-8')
#TypeError: cannot use a string pattern on a bytes-like object
return html
# 我们又创建了getImg()函数,用于在获取的整个页面中筛选需要的图片连接。re模块主要包含了正则表达式:
#
# re.compile() 可以把正则表达式编译成一个正则表达式对象.
#
# re.findall() 方法读取html 中包含 imgre(正则表达式)的数据。
#
# 运行脚本将得到整个页面中包含图片的URL地址。
def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
return imglist
html = getHtml("https://tieba.baidu.com/p/2460150866")
# 把图片地址通过for循环遍历并保存在本地,如下所示:
List = getImg(html)
x=0
for imgurl in List:
urllib.request.urlretrieve(imgurl,'D:\img\%s.jpg' % x)
x+=1
# print(x)
转载于:https://www.cnblogs.com/sincoolvip/p/7274375.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- python爬虫数据-下载图片经典案例
- python爬虫案例1 下载不到图片
- 初学Python爬虫(三):图片下载、微信自动回复和数据可视化
- python实现爬虫下载美女图片
- python爬虫 分页获取图片并下载
- python爬虫案例——糗事百科数据采集
- 一个简单的关于python爬虫获取数据并使用Numpy分析的案例
- python爬虫案例——东方财富股票数据采集
- python脚本工具-1 制作爬虫下载网页图片
- [python][爬虫]从网页中下载图片
- python 爬虫下载图片
- SAS9.3 邮件日志数据经典案例分析(图文并茂版) 可下载
- 【爬虫】添加数据外键下载图片
- 一、python爬虫程序入门(图片下载)
- python简单的图片下载小爬虫
- 爬虫接口数据抓去,下载图片
- python 百度贴吧爬虫(下载图片)
- 使用Python爬虫爬取贴吧图片数据
- Python爬虫——利用PhantomJS下载动态加载图片
- Python爬虫_自动下载图片