1. 程式人生 > >python實現簡單的百度搜索

python實現簡單的百度搜索

python 百度 爬蟲

#!/usr/bin/python
# coding=utf-8
import urllib
import urllib2

#實現百度關鍵字查詢的小例子

#定義基礎url
url = "http://www.baidu.com/s?"

#定義請求頭信息
headers = {"User-Agent" : "Mozilla......"}

#用戶輸入查詢關鍵字
keyword = raw_input("請輸入要查詢的關鍵字:")

#對用戶輸入的關鍵字做變為字典處理
wd = {‘wd‘ : keyword}

#對關鍵字進行url編碼處理
wd = urllib.urlencode(wd)

#拼接完整url
fullurl = url + wd

#構建請求對象
request = urllib2.Request(fullurl,headers = headers)

#請求網頁獲取響應
response = urllib2.urlopen(request)
print(response.read())


python實現簡單的百度搜索