Python谷歌搜索类型

2024-04-24 22:13:34 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,我正在尝试使用Python搜索Google,我发现这段代码大部分是无错误的,但是我得到了错误

(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 )

Tags: 代码inimportjsonurltitleresponserequest
1条回答
网友
1楼 · 发布于 2024-04-24 22:13:34

在一些尝试之后,Google将阻止您的查询,因此响应为“无”,并且您的代码将无法工作

对于网络抓取谷歌结果,您需要一个更复杂的解决方案,使用一系列代理来更改您的IP、管理cookie等

在这里,您将找到更准确的解决方案: Is it ok to scrape data from Google results?

相关问题 更多 >