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

时间:2016-02-25 09:01:54

标签: 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)

响应是:

<Response [200]>
{'responseData': None, 'responseStatus': 403, 'responseDetails': 'This API is no longer available.'}

似乎我正在尝试使用旧API,现在已弃用。当我查看Google Custom Search API时,我看不到任何直接搜索谷歌图片的方式,这是否可以使用新的API?

1 个答案:

答案 0 :(得分: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)

这很好,我只是尝试过。