import requests,re #先把要访问URL和头部准备好 url = 'http://tieba.baidu.com/p/2166231880' head = { 'Accept': '*/*', 'Accept-Encoding':'gzip,deflate,sdch', 'Accept-Language':'zh-CN,zh;q=0.8', 'Connection':'keep-alive' } #获取html源码,这时候html的源码已经在本地了 html = requests.get(url, headers = head) #重新编码 data = html.content.decode('utf-8') i = 0 #用正则匹配找到图片的URL pic_url = re.findall(r'',data,re.S) #把所有图片的URL都列出来,一个一个访问,访问一个存一个。先创建一个0.jpg的文件,然后write写入0.jpg,然后关闭。以此类推 for each in pic_url: print 'Downloading :' + each pic = requests.get(each) fp = open('pic\\' + str(i) + '.jpg','wb') fp.write(pic.content) fp.close() i += 1
运行前:
需要在代码目录下新建一个叫做pic的文件夹,用来存放图片,运行后有惊喜~~~