2017-10-11 102 views
0

嘿,我試着用用Python谷歌搜索的東西,我發現這個代碼,大多出現無誤的,但我得到的錯誤Python的谷歌搜索類型錯誤

(TypeError: 'NoneType' object is not subscriptable)

和不知道是什麼去做。預先感謝您的幫助。

import urllib.request 
import json as m_json 
query = input ('Query: ') 
query = urllib.parse.urlencode ({ 'q' : query }) 
response = urllib.request.urlopen ('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query).read() 
json = m_json.loads (response) 
results = json [ 'responseData' ] [ 'results' ] 
for result in results: 
    title = result['title'] 
    url = result['url'] # was URL in the original and that threw a name error exception 
    print (title + '; ' + url) 

回答

0

經過一些嘗試後,Google會阻止您的查詢,因此回覆爲無,您的代碼將無法工作。

至網絡刮谷歌的結果,你會需要一個更復雜的解決方案使用代理的列表來改變你的IP,管理餅乾等

在這裏你會找到一個更準確的解決了這個: Is it ok to scrape data from Google results?