python3.4爬网页图片
代码
批量自动下载豆瓣网首页图片:
import urllib.request
import re
targetDir = 'D:/Photo ' #文件保存路径
if __name__== '__main__': #程序运行入口
weburl = 'http://www.douban.com'
webheaders = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=weburl, headers=webheaders) #构造请求报头
webpage = urllib.request.urlopen(req) #发送请求报头
contentBytes = webpage.read().decode('utf-8')
imglist=re.findall(r'<img src="([^"]+)"', str(contentBytes))
for each in imglist:
filename =targetDir+ each.split('/')[-1]
urllib.request.urlretrieve(each, filename,None)