使用新的自定义搜索API在Google图片上搜索图片?

xav*_*ier 1 python google-custom-search

因此,我正在测试这段代码:

import requests
import json


searchTerm = 'parrot'
startIndex = '0'
searchUrl = "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + \
    searchTerm + "&start=" + startIndex
r = requests.get(searchUrl)
response = r.content.decode('utf-8')
result = json.loads(response)
print(r)
print(result)
Run Code Online (Sandbox Code Playgroud)

响应是:

<Response [200]>
{'responseData': None, 'responseStatus': 403, 'responseDetails': 'This API is no longer available.'}
Run Code Online (Sandbox Code Playgroud)

似乎我正在尝试使用旧的API,并且现在不推荐使用。当我检查Google自定义搜索API时,看不到任何直接在Google图片上进行搜索的方法,使用新的API甚至可以实现吗?

gal*_*sic 5

有可能,这是新的API参考:https :
//developers.google.com/custom-search/json-api/v1/reference/cse/list

import requests
import json

searchTerm = 'parrot'
startIndex = '1'
key = ' Your API key here. '
cx = ' Your CSE ID:USER here. '
searchUrl = "https://www.googleapis.com/customsearch/v1?q=" + \
    searchTerm + "&start=" + startIndex + "&key=" + key + "&cx=" + cx + \
    "&searchType=image"
r = requests.get(searchUrl)
response = r.content.decode('utf-8')
result = json.loads(response)
print(searchUrl)
print(r)
print(result)
Run Code Online (Sandbox Code Playgroud)

很好,我刚试过。

  • 感谢您的回答 !我是几个小时前才做的,是的,效果很好。您可以进行漂亮的自定义查询,这非常方便,但是每位用户每天100个请求有点低:S如果您想将其自己用于某些(不一定是垃圾邮件)活动,您将发现自己必须破解为了避免付款。 (2认同)