python 爬取图片
import json
import urllib
import urllib2
import os
import requests
def getkey(keyWord):
keyWord=urllib.quote(keyWord)
print(keyWord)
url="https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=cos&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=©right=&word=cos&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&expermode=&force=&pn=60&rn=30&gsm=3c&1551236226042="
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
try:
request=urllib2.Request(url,headers=headers)
open=urllib2.urlopen(request,timeout=20).read()
loads=json.loads(open)
print loads["data"]
return loads["data"]
except urllib2.URLError, e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print e.reason
def getImage(dataList,localPath):
uipath=unicode(localPath,"UTF-8")
if not os.path.exists(uipath):
os.mkdir(uipath)
i=0
for i in range(i,len(dataList)):
try:
path=str(dataList[i]["thumbURL"])
print path
except KeyError,e:
print"没有找到路径"
continue
if path!=None:
print("正在下载 %s"%path )
try:
ir = requests.get(path)
open(uipath + '%d.jpg' % i, 'wb').write(ir.content)
i += 1
except Exception, e:
print '路径显示有问题,无法加载图片'
continue
else:
print '无路径,无法加载图片'
if __name__ == '__main__':
dataList = getkey('cosplay')
getImage(dataList, 'D:/python/FileImage/')