模擬百度進行圖片搜索,有問題能夠留言

模擬百度進行圖片搜索html

import requests
import re
#分析頁面咱們先在百度圖片搜索裏面隨筆輸入一個'背景圖片'得到連接以下
#https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111111&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=背景圖片
#word爲咱們查找的內容


# data = input('請輸入你要搜索的內容')
# rp = requests.get(f'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111111&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word={data}')
# rp.encoding = rp.apparent_encoding
# print(rp.text)
#試了一下很明顯內容是錯誤的,主體不少內容沒有


#這時候咱們加headers
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36'
}
#User-Agent是對於咱們來源的進行假裝

data = input('請輸入你要搜索的內容')
rp = requests.get(f'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111111&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word={data}',headers=headers)
rp.encoding = rp.apparent_encoding
data = '"middleURL":"(.*?)",'
# print(rp.text)
#對於url查找咱們必定不要看原網頁上的,要看訪問請求下來的text,他可能會相比於原來網頁會少了點前綴之類的,可是確定會有一部份內容同樣,絕對能發現的.
url = re.findall(data,rp.text,re.S)
print(url)


#下一步對就是對爬取的url再進行訪問能夠將文件保存至本地,具體能夠參考博客
https://www.cnblogs.com/pythonywy/p/10856508.html
相關文章
相關標籤/搜索