您的位置:首页 > 编程语言 > Python开发

python抓取网页图片的脚本

2015-10-27 10:35 399 查看
首先要在D盘建立pic文件夹然后进入建立beautify的文件夹,然后就可以直接运行python的脚本就可以了

import urllib2, re, requests
path = r"D:\pic\beautify"
url = 'http://huaban.com/favorite/beauty'
i_headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36"}
count=0

def urlHandle(url):
req = urllib2.Request(url, headers=i_headers)
html = urllib2.urlopen(req).read()
reg = re.compile(r'"pin_id":(\d+),.+?"file":{"farm":"farm1", "bucket":"hbimg",.+?"key":"(.*?)",.+?"type":"image/(.*?)"', re.S)
groups = re.findall(reg, html)
return groups

def imgHandle(groups):
if groups:
for att in groups:
pin_id = att[0]
att_url = att[1] + '_fw236'
img_type = att[2]
img_url = 'http://img.hb.aicdn.com/' + att_url

r = requests.get(img_url)
with open(path + att_url + '.' + img_type, 'wb') as fd:
for chunk in r.iter_content():
fd.write(chunk)

groups = urlHandle(url)
imgHandle(groups)

while(groups):
count+=1
print count
pin_id  = groups[-1][0]
print pin_id
urltemp = url+'/?max=' + str(pin_id) + '&limit=' + str(20) + '&wfl=1'
print(urltemp)
groups = urlHandle(urltemp)
imgHandle(groups)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: