import requests
import re
import os
word =input('请输入要爬取的内容:')
if not os.path.exists(word):
os.mkdir(word)
i=0
num=int(input("请输入要爬取的页数(每页30张图片):"))
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
urls=['https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%A3%81%E7%BA%B8&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=©right=&word='+word+'&pn={}'.format(j) for j in range(30,num*30+1,30)]
def get_img(url):
response=requests.get(url,headers)
content=response.content.decode('utf-8')
img_urls=re.findall('"thumbURL":"(.*?)"',content,re.S)
global i
for img_url in img_urls:
response=requests.get(img_url,headers)
content=response.content
with open(word+'/'+'{}.jpg'.format(i+1),'wb') as f:
f.write(content)
print("正在爬取第{}张图片".format(i+1))
i+=1
for url in urls:
get_img(url)
百度图片爬虫
最新推荐文章于 2024-11-08 13:43:44 发布