步驟分析:
1. 偽裝瀏覽器的 User-Agent
2. 讀取輸入?yún)?shù)并組裝為請(qǐng)求參數(shù)
3. 使用 `requests` 模塊請(qǐng)求數(shù)據(jù)
4. 使用 `bs4` 查找需要的鏈接數(shù)據(jù)
5. 使用`webbrowser`模塊 打開鏈接望伦。
代碼如下:
# -*- coding: UTF-8 -*-
import requests, sys, webbrowser, bs4
#偽裝瀏覽器頭
def camouflageWrowser():
reload(sys)
sys.setdefaultencoding('utf-8')
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'}
return headers
# 讀取輸入?yún)?shù)并組裝為搜索用的參數(shù)
def readInputParameterAndFormat():
searchWorld = ' '.join(sys.argv[1:])
payload = {'wd':u'%s'%searchWorld}
print("searchWorld = " + searchWorld)
return payload
# 搜索關(guān)鍵字
def requestsWithParameter(headers,parameter):
url = 'http://www.baidu.com/s'
r = requests.get(url, params=parameter, headers=headers, timeout=5)
return r
# 查找需要的內(nèi)容
def findNeedResult(request):
# 查找需要的內(nèi)容
soup = bs4.BeautifulSoup(request.text,"html.parser")
linkElems = soup.select('.result > .t > a[href]')
topLinkElems = soup.select('.result-op > .t > a[href]')
return linkElems + topLinkElems
# 打開全部搜索結(jié)果
def openElement(el):
webbrowser.open(el.get('href'))
#偽裝瀏覽器頭
headers = camouflageWrowser()
# 讀取輸入?yún)?shù)
payload = readInputParameterAndFormat()
# 請(qǐng)求關(guān)鍵字?jǐn)?shù)據(jù)
r = requestsWithParameter(headers,payload)
# 解析返回的結(jié)果
linkElems = findNeedResult(r)
for el in linkElems:
openElement(el)
這個(gè)是好久之前的了。许布。Python3 的寫法跟這個(gè)又不一樣苍凛。
倒入 import importlib
然后把 reload(sys)
替換我 importlib.reload(sys)
sys.setdefaultencoding('utf-8')
這行代碼可以不要了
# -*- coding: UTF-8 -*-
import requests, sys, webbrowser, bs4
import importlib
#偽裝瀏覽器頭
def camouflageWrowser():
#reload(sys)
importlib.reload(sys)
#sys.setdefaultencoding('utf-8')
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'}
return headers
# 讀取輸入?yún)?shù)并組裝為搜索用的參數(shù)
def readInputParameterAndFormat():
searchWorld = ' '.join(sys.argv[1:])
payload = {'wd':u'%s'%searchWorld}
print("searchWorld = " + searchWorld)
return payload
# 搜索關(guān)鍵字
def requestsWithParameter(headers,parameter):
url = 'http://www.baidu.com/s'
r = requests.get(url, params=parameter, headers=headers, timeout=5)
return r
# 查找需要的內(nèi)容
def findNeedResult(request):
# 查找需要的內(nèi)容
soup = bs4.BeautifulSoup(request.text,"html.parser")
linkElems = soup.select('.result > .t > a[href]')
topLinkElems = soup.select('.result-op > .t > a[href]')
return linkElems + topLinkElems
# 打開全部搜索結(jié)果
def openElement(el):
webbrowser.open(el.get('href'))
#偽裝瀏覽器頭
headers = camouflageWrowser()
# 讀取輸入?yún)?shù)
payload = readInputParameterAndFormat()
# 請(qǐng)求關(guān)鍵字?jǐn)?shù)據(jù)
r = requestsWithParameter(headers,payload)
# 解析返回的結(jié)果
linkElems = findNeedResult(r)
for el in linkElems:
openElement(el)