【问题标题】:Google Search by Image in Python在 Python 中按图像进行 Google 搜索
【发布时间】:2015-07-28 18:17:10
【问题描述】:

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

【问题讨论】:

  • 您的浏览器使用 HTTP GET/POST 访问它。如果你在 Python 中使用 requests 模块,你应该能够模拟你的浏览器是如何做到的。

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


【解决方案1】:

以下代码 sn-p 显示了如何使用 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...

【讨论】:

  • 使用此链接了解更多信息developers.google.com/image-search/v1/jsondevguide
  • 仅供参考 "Google Image Search API 已于 2011 年 5 月 26 日正式弃用。根据我们的弃用政策,它将继续工作,但您每天可以提出的请求数量可能会受到限制. 我们鼓励您使用现在支持图像搜索的自定义搜索 API。”所以不要将它用于任何重要的事情,因为它可能不会存在太久。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多