python requests抓取百度图片并保存至本地文件夹

2018-01-25  本文已影响0人  爱代码的派派星

-- coding:utf-8 --

import requests
import re
import urllib
import sys,os
type=sys.getfilesystemencoding()

print type

key= raw_input('what do you want?')
print key
key=key.decode('cp936').encode('utf-8')

html = requests.get("http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1516787011528_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%s"%key)
print html.url
print html.text

html.encoding='utf-8'
page=html.text

os.mkdir('pic') 1516849068(1).png

def getimg(page):
reg = r'"objURL":"(.*?)",'
imgre = re.compile(reg)
imglist = re.findall(imgre,page)
#print imglist
x=0
for i in imglist:
urllib.urlretrieve(i,'C:\Python27\pictures\%s.jpg'%x)
x+=1
print x
getimg(page)

上一篇下一篇

猜你喜欢

热点阅读