python抓取网站的图片并下载到本地的方法
实例如下所示:
#!/usr/bin/python # -*- coding: UTF-8 -*- import re import urllib,urllib2; #通过url获取网页 def getHtml(url): # 要设置请求头,让服务器知道不是机器人 user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = {'User-Agent': user_agent} request=urllib2.Request(url,headers=headers); page = urllib2.urlopen(request); html = page.read() return html #通过正则表达式来获取图片地址,并下载到本地 def getImg(html): reg = r'src="(.+?\.jpg)"' imgre = re.compile(reg) imglist = imgre.findall(html) x = 0 for imgurl in imglist: print imgurl; #通过urlretrieve函数把数据下载到本地的D:\\images,所以你需要创建目录 urllib.urlretrieve(imgurl, 'D:\\images\\%s.jpg' % x) x = x + 1 html = getHtml("http://www.qiushibaike.com/imgrank/") getImg(html)
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16