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甚至可以实现吗?
有可能,这是新的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)
很好,我刚试过。