import re # 正则表达式库
import requests # requests库
# from bs4 import BeautifulSoup #BeautifulSoup库
# 百度图片网站链接
url = 'https://image.baidu.com/search/index'
keywords = input('请输入要查找的图片:')
# 网站参数
param = {
'tn': 'baiduimage',
'ps': 1,
'ct': 201326592,
'lm': -1,
'cl': 2,
'nc': 1,
'ie': 'utf-8',
'word': keywords
}
# 网站请求标头
header = {
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
'Access-Control-Allow-Credentials': 'true',
'Connection': 'keep-alive',
'Accept-Language': 'zh-CN,zh;q=0.9'
}
# 解析网站
r = requests.get(url, params=param, headers=header)
link = re.findall('"thumbURL":"(.*?)"', r.text)
count = 0
# 打印所有图片链接
for i in link:
print(i)
# 保存图片
for j in link:
count += 1
res = requests.get(j, headers=header)
with open('F:\桌面\图片\s{}.jpg'.format(count), 'wb') as fp:
fp.write(res.content)
r.close()
res.close()