通过pip安装
cmd输入:pip install requests
import json
import os
import requests
def downloadImage(keyword,begin,end,file):
id=0;
if not os.path.exists(file): # 目录不存在时,创建目录
os.makedirs(file)
for i in range(begin*30,end*30,30):
data = ({
'tn': 'resultjson_com',
'ipn': 'rj',
'queryWord': keyword,
'word': keyword,
'pn': i,
'rn': 30
})
url = 'https://image.baidu.com/search/acjson'
req=requests.get(url,params=data).json().get("data");
for it in req:
if 'thumbURL' in it and len(it['thumbURL'])>0:
print('正在下载:%s' % it['thumbURL'])
img = requests.get(it['thumbURL'])
open(file + '%d.jpg' % id, 'wb').write(img.content)
id += 1
else:
print('链接已失效')
if __name__ == '__main__':
dataList = downloadImage('星空',0,100,'d:/myImage/') # (关键字,起始页数,终止页数(30张/页),存储目录)