天天看點

python模拟百度搜尋

import requests

def baidu_search(wd):
    url = 'https://www.baidu.com/s'
    UserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
    headers = {'User-Agent':UserAgent}
    params = {"ie": "utf-8","f": 8,"rsv_bp": 1, "rsv_idx": 1,
              "tn": "baidu","wd": wd, "oq": "%E7%A0%81%E9%80%9A",
              "rsv_pq": "83d9357300002cde",
              "rsv_t": "1a90FbmG3VtA2Hj/O6Ao6MFz+vLsZ6iseHVPJtOu4dPO0X5EGstRR+3XCz4",
              "rqlang": "cn","rsv_enter": 1,"rsv_dl": "tb",
              "rsv_sug3": 6,"rsv_sug1": 2,"rsv_sug7": 100,
              "rsv_sug2": 0,"inputT": 1279,"rsv_sug4": 2909
              }
    r = requests.get(url,headers = headers, params = params)
    print(r.url)

    file_name = 'baidu.html'
    with open(file_name, 'w') as f:
        f.write(r.text)
wd = input("請輸入要查詢的關鍵字:")
baidu_search(wd)