Md.*_*lam 70 google-api google-image-search
我正在使用谷歌图片搜索API.直到昨天它正在运行,但今天早上它说"此API不再可用"
是正式关闭,还是我身边的任何错误
请求
https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q=cute+kittens
响应
{"responseData": null, "responseDetails": "This API is no longer available.", "responseStatus": 403}
Run Code Online (Sandbox Code Playgroud)
Vij*_*kar 109
我找到的答案是使用谷歌的自定义搜索引擎(CSE)API.请注意,这仅限于每天100次免费请求.
cx
和修改它以搜索图像cx
在浏览器中找到URL.API端点网址是 https://www.googleapis.com/customsearch/v1
以下JSON参数用于此API:
q
:指定搜索文本num
:指定结果数.需要1到10之间的整数值(含)start
:结果的"偏移量",这导致搜索应该从.需要1到101之间的整数值.imgSize
:图像的大小.我用了"medium"
searchType
:必须设置为 "image"
filetype
:指定图像的文件类型.我使用了"jpg",但如果文件扩展名对您无关紧要,可以将其保留.key
:API密钥,从https://console.developers.google.com/获取cx
:上一节中的自定义搜索引擎ID只需GET
通过将上述参数作为JSON传递给API端点(也在上面列出)来发出请求.
注意:如果您在搜索引擎设置中设置了引荐来源列表,则可能无法通过浏览器访问该网址.您需要从此列表中指定的服务器进行AJAX调用(或来自其他语言的等效调用).它仅适用于配置设置中指定的引荐来源.
参考:https: //developers.google.com/custom-search/json-api/v1/reference/cse/list
aft*_*4ik 23
现在您可以使用自定义图像搜索API搜索图像.
您可以通过两个步骤完成此操作:
1)获取CUSTOM_SEARCH_ID
转到 - https://cse.google.ru/cse/all
在这里,您必须创建新的搜索引擎.这样做并在那里启用图像搜索.
屏幕(我是俄罗斯人...对不起)
然后获取此搜索引擎ID.要执行此操作,请按"获取代码"按钮:
并找到cx ="这里将是你的CUSTOM_SEARCH_ID"的行:
好.它已经完成,现在是第二步:
2)获取SERVER_KEY
转到Google控制台 - https://console.developers.google.com/project
按下创建项目按钮,输入名称和其他所需信息.选择此项目并转到启用Apis
现在找到自定义搜索引擎.
现在我们必须转到凭据并创建新的服务器密钥:
好.现在我们可以使用图片搜索了.
查询:
https://www.googleapis.com/customsearch/v1?key=SERVER_KEY&cx=CUSTOM_SEARCH_ID&q=flower&searchType=image&fileType=jpg&imgSize=xlarge&alt=json
Run Code Online (Sandbox Code Playgroud)
替换SERVER_KEY和CUSTOM_SEARCH_ID并调用此请求.
限制:免费,您每天只能搜索100张图片.谢谢你的关注.
如果这仅仅是出于您自己的目的(而非用于制作)并且您不打算滥用Google图片搜索,则只需使用JSOUP从Google搜索结果中提取第一张图片网址即可.
例如: 用于检索第一个缩略图的图像URL的代码:
public static String FindImage(String question, String ua) {
String finRes = "";
try {
String googleUrl = "https://www.google.com/search?tbm=isch&q=" + question.replace(",", "");
Document doc1 = Jsoup.connect(googleUrl).userAgent(ua).timeout(10 * 1000).get();
Element media = doc1.select("[data-src]").first();
String finUrl = media.attr("abs:data-src");
finRes= "<a href=\"http://images.google.com/search?tbm=isch&q=" + question + "\"><img src=\"" + finUrl.replace(""", "") + "\" border=1/></a>";
} catch (Exception e) {
System.out.println(e);
}
return finRes;
}
Run Code Online (Sandbox Code Playgroud)
指南:
问题 - 图像搜索术语
ua - 浏览器的用户代理
在我阅读了几个回复后,我用图片编译了一个回复:
<html lang="pt">
<head>
<title>JSON Custom Search API Example</title>
</head>
<body>
<div id="content"></div>
<script>
function hndlr(response) {
console.log(response);
for (var i = 0; i < response.items.length; i++) {
var item = response.items[i];
// in production code, item.htmlTitle should have the HTML entities escaped.
document.getElementById("content").innerHTML += "<br>" + item.htmlTitle;
}
}
</script>
<script src="https://www.googleapis.com/customsearch/v1?key=API_KEY&cx=SEARCH_ENGINE_KEY&q=flower&searchType=image&callback=hndlr"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
基本代码可在此处找到:https : //developers.google.com/custom-search/v1/using_rest
感谢@Vijay Shegokar、@aftamat4ik 和@Alladinian
这是要使用的完整 URL 模板
我们可以删除不必要的参数。
https://www.googleapis.com/customsearch/v1?q= {searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref ={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={ hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict? }&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor= {imgDominantColor?}&alt=json
我在用
归档时间: |
|
查看次数: |
74742 次 |
最近记录: |