# -*- coding: utf-8 -*-
import requests
import time
import os
import sys
import importlib
import json
importlib.reload(sys)
def getManyPages(keyword,pages):
params=[]
for i in range(30,30*pages+30,30):
params.append({
'tn': 'resultjson_com',
'ipn': 'rj',
'ct': 201326592,
'is': '',
'fp': 'result',
'queryWord': keyword,
'cl': 2,
'lm': -1,
'ie': 'utf-8',
'oe': 'utf-8',
'adpicid': '',
'st': -1,
'z': '',
'ic': 0,
'word': keyword,
's': '',
'se': '',
'tab': '',
'width': '',
'height': '',
'face': 0,
'istype': 2,
'qc': '',
'nc': 1,
'fr': '',
'pn': i,
'rn': 30,
'gsm': '1e',
'1488942260214': ''
})
url = 'https://image.baidu.com/search/acjson'
urls = []
for i in params:
try:
urls.append(requests.get(url, params=i).json().get('data'))
except json.decoder.JSONDecodeError:
print("解析出错")
return urls
def getImg(dataList, localPath):
if not os.path.exists(localPath):
os.mkdir(localPath)
x = 1
for list in dataList:
for i in list:
if i.get('thumbURL') != None:
print('正在下载:%s' % i.get('thumbURL'))
ir = requests.get(i.get('thumbURL'))
string = localPath + '/' + '%04d.jpg' % x
with open(string, 'wb') as f:
f.write(ir.content)
x += 1
time.sleep(0.5)
else:
print('图片链接不存在')
if __name__ == '__main__':
keyword = input("输入关键词:")
dataList = getManyPages(keyword, 100)
getImg(dataList,'E:\\data\\Mydata\\'+keyword)