python正则下载图片
import urllib.request import re # 打开图片路径 def open_url(url): #设置请求路径 req = urllib.request.Request(url) #设置请求头 req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36') #发送请求 response = urllib.request.urlopen(req) #获取返回内容 page = urllib.request.urlopen(req) html = page.read().decode('utf-8') return html # 获取图片路径 def get_img(html): p = '<img class="BDE_Image" src="([^"]+\.jpg)' imglist = re.findall(p,html) ''' for each in imglist: print(each) ''' for each in imglist: filename = each.split("/")[-1] urllib.request.urlretrieve(each,filename,None) #主函数 if __name__ == "__main__": url = "http://tieba.baidu.com/p/5838400549" get_img(open_url(url))
用心做人,用脑做事!----鹏鹏张