資源簡介
百度圖片爬蟲(python版)
代碼片段和文件信息
import?urllib.request
import?urllib.parse
import?re
import?os
#?添加header,其中Referer是必須的否則會返回403錯誤,User-Agent是必須的,這樣才可以偽裝成瀏覽器進行訪問
header?=?\
????{
????????‘User-Agent‘:?‘Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML?like?Gecko)?Chrome/56.0.2924.87?Safari/537.36‘
????????“referer“:?“https://image.baidu.com“
????}
url?=?“https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={word}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word={word}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&cg=girl&pn={pageNum}&rn=30&gsm=1e00000000001e&1490169411926=“
keyword?=?input(“請輸入想要爬取的圖片搜索關鍵字:“)
#?轉碼
keyword?=?urllib.parse.quote(keyword?‘utf-8‘)
n?=?0
j?=?0
while?(n?3000):
????error?=?0
????n?+=?30
????#?url
????url1?=?url.format(word=keyword?pageNum=str(n))
????#?獲取請求
????rep?=?urllib.request.Request
評論
共有 條評論