谷歌搜索Python中的图像

时间:2015-07-28 18:17:10

标签: python json api google-api google-custom-search

有没有办法在Python中使用Google搜索功能?我正在考虑从指定位置获取图像文件并使用该文件来获取相关的搜索结果。 我查看了Google自定义搜索API,似乎无法找到有关此主题的任何内容。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下代码段显示了如何使用Python向Google Image Search API发出请求。此示例假定为Python 2.4或更高版本。您可能需要下载并安装simplejson。

import urllib2
import simplejson

url = ('https://ajax.googleapis.com/ajax/services/search/images?' +
       'v=1.0&q=barack%20obama&userip=INSERT-USER-IP')

request = urllib2.Request(url, None, {'Referer': /* Enter the URL of your site here */})
response = urllib2.urlopen(request)

# Process the JSON string.
results = simplejson.load(response)
# now have some fun with the results...
相关问题