1. 程式人生 > >python 模擬百度搜索

python 模擬百度搜索

 1 import urllib.request
 2 def Url(url):
 3     flag = input("請輸入要搜尋的關鍵字:")
 4     headers_ = {
 5         "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36"
 6     }
 7     wd = {"wd" : flag}
 8     wd = urllib.parse.urlencode(wd)
9 url = url + "?" + wd 10 print(url) 11 req = urllib.request.Request(url,headers=headers_) 12 html = urllib.request.urlopen(req).read().decode("utf-8") 13 print(html) 14 15 16 17 url = 'http://www.baidu.com/s' 18 Url(url)