ai didi

python - 为什么通过 simplejson 的 Google API 查询返回 "responseData": null?

转载 作者:行者123 更新时间:2023-12-01 06:16:29 24 4
gpt4 key购买 nike

我正在尝试使用 Python 和 simplejson 截屏 Google 搜索的第一个结果,但我无法像许多在线示例演示的那样访问搜索结果。这是一个片段:

url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % (query)
search_results = urllib.urlopen(url)
json = simplejson.load(search_results)
try:
results = json['responseData']['results'] # always fails at this line
first_result = results[0]
except:
print "attempt to set results failed"

当我访问http://ajax.googleapis.com/ajax/services/search/web?v=1.0&stackoverflow时(或任何其他替代 %s 的内容)在浏览器中,它会显示行“{”responseData”:null,“responseDetails”:“clip swinging”,“responseStatus”:204}。除了尝试使用明显空的responseData之外,还有其他方法可以访问Python中的Google搜索结果吗?

最佳答案

您错过了 &q=。您还应该考虑使用 api-key。 http://code.google.com/intl/de/apis/ajaxsearch/documentation/ 。除了纯字符串接触不起作用之外,您还需要转义参数。

 url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q= ' + urllib.quote_plus(query)

关于python - 为什么通过 simplejson 的 Google API 查询返回 "responseData": null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3103116/

24 4 0
文章推荐: python - 用 Python 控制音乐和视频
文章推荐: python - gtk treeview - 具有实时更新的列表存储
文章推荐: jquery - 使用 jQuery 恢复到页面的原始 CSS
文章推荐: java.lang.UnsatisfiedLinkError - native 方法问题
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com