学习百度图片爬取
import requestsimport re
import os
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'}
# url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + name + '&pn=' + str(i * 60)
num = 0
name = input('输入查找的图片:')
x = int(input('输入1等60张,请输入需要的数量:'))
path = 'F:\\百度图片爬取\\'
if not os.path.exists(path):
os.mkdir(path)
for i in range(x):
url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + name + '&pn=' + str(i * 60)
res = requests.get(url, headers=headers)
html = res.content.decode()
lst = re.findall('"objURL":"(.*?)",', html)
for j in lst:
num += 1
try:
img = requests.get(j)
except Exception as e:
print(e)
continue
f = open(path + name + str(num) + '.jpg', 'wb')
print('-----------第'+str(num)+"张在下载------------")
f.write(img.content)
f.close()
print('完成') 本帖最后由 192666378 于 2021-4-27 12:53 编辑
失败……有验证码了
页:
[1]