import os
import requests
import re
num = 0
numPicture = 0
file = ''
List = []
# global num
global num1
def dowmloadPicture(html, keyword):
global num
# t =0
pic_url = re.findall('"objURL":"(.*?)",', html, re.S) # 先利用正则表达式找到图片url
if not os.path.exists('./' + keyword):
os.mkdir('./' + keyword)
path = './' + keyword
print('找到关键词:' + keyword + '的图片,即将开始下载图片...')
for each in pic_url:
if num < int(num1):
print('正在下载第' + str(num + 1) + '张图片,图片地址:' + str(each))
try:
if each is not None:
pic = requests.get(each, timeout=7)
else:
continue
except BaseException:
print('错误,当前图片无法下载')
continue
else:
string = path + '_' + str(num) + '.jpg'
with open(os.path.join(keyword,string), 'wb') as fp:
fp.write(pic.content)
fp.close()
num += 1
if __name__ == '__main__': # 主函数入口
header = {'content-type': 'application/json','User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
word = input("请输入搜索关键词: ")
num1 = input("请输入下载数量: ")
# while 1:
url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&pn='
r = requests.get(url + num1, headers=header, allow_redirects=False)
r.encoding = 'utf-8'
dowmloadPicture(r.text, word)
# num1 = num1+20
直接启动程序,输入信息
等待下载即可。