1.獲取百度搜索結果頁面主要是修改百度搜索url中的參數實現,例如查詢的關鍵字為wd;
舉例:https://www.baidu.com/s?wd=python",這樣就可以查詢到‘python’相關的內容
具體的參數屆時可以參考:https://blog.csdn.net/ZustKe/article/details/83882345
2.通過python獲取百度內容時,會出現返回的頁面內容是“百度安全驗證”的情況,像下面這樣
這是因為設置header是沒有設置accept參數,設置后就OK了。
慣例附代碼:
import urllib.request headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 Edg/83.0.478.50', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' } url = "https://www.baidu.com/s?wd=python" req = urllib.request.Request(url=url, headers=headers) html = urllib.request.urlopen(req).read().decode('UTF-8', 'ignore') print(html)