Python爬虫之抓取网络图片

释放双眼,带上耳机,听听看~!

1.目的

以 百度图片首页为例,首页如下图所示,网页上有一些图片,我们的目的就是将这些图片保存到本地。

Python爬虫之抓取网络图片

2.源码


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1#coding=utf-8
2#version: python 2.7
3#author: Hao Chen
4
5import urllib
6import re
7
8#step1.获取整个页面的数据
9url="http://image.baidu.com/"
10page = urllib.urlopen(url)     #打开一个url地址
11html = page.read()             #读取url上的数据
12
13#step2.删选页面中想要的数据
14reg = r'src="(.+?\.jpg)" '        #构建正则表达式
15imgre = re.compile(reg)           #把正则表达式变异成一个对象
16imgList = re.findall(imgre,html)  #读取html中包含正则表达式的数据
17
18#直接用以下方法也行,更简便
19#imgList = re.findall('src="(.+?\.jpg)" ',html)
20
21#step3.将页面筛选的数据保存到本地
22x=0
23for imgurl in imgList:
24    urllib.urlretrieve(imgurl,'%s.jpg'%x)  #远程将数据下载到本地
25    x+=1
26
27

给TA打赏
共{{data.count}}人
人已打赏
安全经验

职场中的那些话那些事

2021-9-24 20:41:29

安全经验

nginx日志分析利器GoAccess

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索