我有一個相對較大的項目,其中搜索Google已返回缺失值的最佳結果。在Python中使用谷歌搜索可以得到我需要的確切結果。當試圖使用自定義搜索爲了解除我的查詢限制時,返回的結果不是遠程接近我需要的。我有以下代碼(在Searching in Google with Python建議)返回正是我需要的,這是完全一樣的事情,當我在谷歌的網站上搜索,但被阻止,由於過多的http請求......將Google自定義搜索配置爲像google.search一樣工作()
from google import search
def google_scrape(url):
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
thepage = opener.open(url)
soup = BeautifulSoup(thepage, "html.parser")
return soup.title.text
i = 1
# queries = ['For. Policy Econ.','Int. J. Soc. For.','BMC Int Health Hum. Rights',
# 'Environ. Health Persp','Environ. Entomol.','Sociol. Rural.','Ecol. Soc.']
search_results = []
abbrevs_searched = []
url_results = []
error_names = []
error = []
#Note, names_to_search is simply a longer version of the commented our queries list.
for abbreviation in names_to_search:
query = abbreviation
for url in search(query, num=2,stop=1):
try:
a = google_scrape(url)
print(str(i) + ". " + a)
search_results.append(a)
abbrevs_searched.append(query)
url_results.append(url)
print(url)
print(" ")
except Exception as e:
error_names.append(query)
error.append(query)
print("\n\n***************"," Exeption: ",e)
i += 1
而且我在下面的方式我的谷歌自定義搜索引擎代碼設置...
import urllib
from bs4 import BeautifulSoup
import http.cookiejar
from apiclient.discovery import build
"""List of names to search on google"""
names_to_search = set(search_list_1+search_list)
service = build('customsearch', 'v1',developerKey="AIz**********************")
rse = service.cse().list(q="For. Policy Econ.",cx='*******************').execute()
rse
我的谷歌自定義搜索引擎設置都設置爲搜索Google.com。截至目前,所有其他設置都是默認的,除了網站是Google.com之外。
你使用哪種谷歌搜索API? – ands
我在第一個示例中使用了Google的Python包中的搜索,該示例的工作方式如下,並且我在第二個示例中通過我的個人應用程序使用了自定義搜索引擎,該搜索引擎沒有返回我想要的內容。我更新了導入以正確反映每個給定的代碼塊。 – Min
你會得到哪個錯誤? – ands