Python爬取图片
今天我写的一个小爬虫是爬取图片
利用python爬虫爬取图片,首先要进行安装python,
在(https://www.python.org/)进行下载python安装
安装python之后,在进行安装第三方库,requests,bs4,lxml这三个库。。。
在cmd中
进行安装pip install requests,输入后回车,另外两个一样,等安装好后就可以了。。。
python爬取图片现在开始:
利用python抓取网络图片的步骤:
1.根据给定的网址获取网页源代码
2.利用正则表达式把源代码中的图片地址过滤出来
3.根据过滤出来的图片地址下载网络图片
今天我们用(http://www.mzitu.com/zipai/comment-page-1/)作为事例,教大家爬取美女图片:
**1:打开浏览器,输入网址(http://www.mzitu.com/zipai/comment-page-1/),进入网页
3:看到网页源代码后,首先要获取网页源代码
res = requests.get('http://www.mzitu.com/zipai/comment-page-1) # 响应
print(res.text) # 打印网页源代码
4:获取源代码后,提取url,获取每页图片url
href = re.findall(r’<img src=“(.?)" alt=".?”', res.text) # 筛选
print(href) #打印出一页的所有图片url
5:获取之后就可以下载,保存图片了 ,,想想就激动。直接写到一个方法里就可以了,
def imgurl(url):
for i in url:
# meiziid = url[-14:-11] # 切片将src的倒数的字符串做名字
# print('开始下载妹子:', format