(我試過尋找,但所有其他的答案似乎是使用的urllib2)與Python庫的請求谷歌搜索
我剛剛開始嘗試使用要求,但我仍然不是很清楚的如何發送或從頁面請求額外的東西。例如,我會
import requests
r = requests.get('http://google.com')
,但我不知道怎麼了,例如,使用做介紹的搜索欄谷歌搜索。我已閱讀快速入門指南,但對HTML POST等不太熟悉,所以它並沒有太大的幫助。
有沒有一種乾淨而優雅的方式來做我所要求的?
(我試過尋找,但所有其他的答案似乎是使用的urllib2)與Python庫的請求谷歌搜索
我剛剛開始嘗試使用要求,但我仍然不是很清楚的如何發送或從頁面請求額外的東西。例如,我會
import requests
r = requests.get('http://google.com')
,但我不知道怎麼了,例如,使用做介紹的搜索欄谷歌搜索。我已閱讀快速入門指南,但對HTML POST等不太熟悉,所以它並沒有太大的幫助。
有沒有一種乾淨而優雅的方式來做我所要求的?
請求概述
的谷歌搜索請求是標準的HTTP GET命令。它包含與您的查詢相關的參數集合。這些參數包含在請求URL中,作爲由&符號分隔的名稱=值對(&)字符。參數包括類似搜索查詢的數據和標識發出HTTP請求的CSE的唯一CSE ID(cx)。 WebSearch或Image Search服務返回XML結果以響應您的HTTP請求。
首先,你必須在Control Panel of Custom Search Engine
然後讓你的CSE ID(CX參數),See the official Google Developers site for Custom Search.
有許多例子是這樣的:
http://www.google.com/search?
start=0
&num=10
&q=red+sox
&cr=countryCA
&lr=lang_fr
&client=google-csbe
&output=xml_no_dtd
&cx=00255077836266642015:u-scht7a-8i
而且有解釋的列表您可以使用的參數。
import requests
def googlesearch(searchfor):
link = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % searchfor
ua = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36'}
payload = {'q': searchfor}
response = requests.get(link, headers=ua, params=payload)
print response.text
googlesearch('test')
打印:
{ 「responseData」:{ 「結果」:[{ 「GsearchResultClass」: 「GwebSearch」, 「unescapedUrl」: 「http://www.speedtest.net/」, 「URL」: 「http://www.speedtest.net/」 ,「visibleUrl」:「www.speedtest.net」,「cacheUrl」:「http://www.google.com/search?q \ u003dcache:M47_v0xF3m8J:www.speedtest.net」,「title」:「Speedtest.net by Ookla - 全球寬帶速度測試\ u003c/b \ u003e「,」titleNoFormatting「:」Speedtest.net by Ookla - 全球寬帶速度測試「,」內容「:」\ u003cb \ u003c測試\ /您的互聯網連接帶寬到世界各地用這種\ n互動寬帶速度\ u003cb \ ueeetest \ u從Ookla 003C/B \ u003e「},
等
「Google Web Search API不再可用,請遷移到Google自定義搜索API」。 – mootmoot
這不起作用:'{「responseData」:null,「responseDetails」:「Google Web Search API不再可用,請遷移到Google Custom Search API(https://developers.google.com/custom -search /)「,」responseStatus「:403}' – tumbleweed
輸入:
import requests
def googleSearch(query):
with requests.session() as c:
url = 'https://www.google.co.in'
query = {'q': query}
urllink = requests.get(url, params=query)
print urllink.url
googleSearch('Linkin Park')
輸出:
https://www.google.co.in/?q=Linkin+Park
import requests
from bs4 import BeautifulSoup
headers_Get = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
}
def google(q):
s = requests.Session()
q = '+'.join(q.split())
url = 'https://www.google.com/search?q=' + q + '&ie=utf-8&oe=utf-8'
r = s.get(url, headers=headers_Get)
soup = BeautifulSoup(r.text, "html.parser")
output = []
for searchWrapper in soup.find_all('h3', {'class':'r'}): #this line may change in future based on google's web page structure
url = searchWrapper.find('a')["href"]
text = searchWrapper.find('a').text.strip()
result = {'text': text, 'url': url}
output.append(result)
return output
將返回Google'{'text':text,'url':url}格式的結果數組。最佳結果網址爲google('search query')[0]['url']
您可以在沒有客戶端庫的情況下使用Google API。我在使用urllib.request模塊的Python 3中使用Google Drive。 – Trimax
好吧,我不是僅僅在Google的背景下,它還有其他網站/數據庫,我也希望能夠搜索。 另外,我認爲現在的標準是請求模塊,因爲urllib/urllib2變得笨重/過時了嗎? – James
一些方法(GET)通過url傳遞它們的參數,其他(POST)通過數據傳遞它們的參數。並且都承認標題(配對或關鍵字和值) – Trimax