import requests
import re
from bs4 import BeautifulSoup
import os
import time
key=input("請輸入您要查詢的內容:") # 獲取想要搜索的資訊
mz= key
os.mkdir('C:\\Users\\Lenovo\\Desktop\\' + mz)#在桌面建立一個你輸入搜索關鍵詞的檔案夾
for page in range(0,120,30):#動態網頁步數每一步30遞增,我設定120除以30=4個動態頁面,每個頁碼抓30張合計就是120張左右,你也可以多設定幾個頁碼就是30的倍數。
url = 'https://image.baidu.com/search/index?tn=baiduimage&ipn=utf-8&sid=&word='+ (key) + '&pn={}'.format(str(page))
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
data = requests.get(url, headers=headers)
text = requests.get(url).text
print(url.format(page))
image = re.findall('"hoverURL":"(.*?)"',text)#圖片名字
for i in image:
time.sleep(0.2)#爬取時間間隔
imgname = i.split('/')[-1]#圖片名檢索
tp = requests.get(i,headers = headers)#圖片決議
with open('C:\\Users\\Lenovo\\Desktop\\' + mz + '\\'+imgname,'wb') as file: #保存本地檔案夾位置并轉二進制
file.write(tp.content)#圖片二進制決議以后保存
print(imgname)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/21418.html
