Javascript 仅 Google 自定义图片搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8448788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google custom search for images only
提问by sibidiba
Since Google image search APIis deprecated, one should use Google custom search APIfor this.
由于不推荐使用Google 图像搜索 API,因此应为此使用Google 自定义搜索 API。
I've made a small example using it. My problem is I want to return google image search results only. Whereby this shows web results, and the user may switch to the image result. How can I show only the image results by default?
我用它做了一个小例子。我的问题是我想回到谷歌图片搜索结果只。从而显示网页结果,用户可以切换到图像结果。如何默认只显示图像结果?
<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'hu'});
google.setOnLoadCallback(function() {
var customSearchOptions = {
enableImageSearch: true,
imageSearchOptions: {
layout: google.search.ImageSearch.LAYOUT_CLASSIC
}
};
var options = new google.search.DrawOptions();
options.setAutoComplete(true);
var customSearchControl = new google.search.CustomSearchControl('XXX', customSearchOptions);
customSearchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
customSearchControl.setAutoCompletionId('XXX');
customSearchControl.draw('cse', options);
}, true);
</script>
<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />
The API documentation is quite poor, it only describes how to add additional results.
API 文档很差,只描述了如何添加额外的结果。
回答by kelorek
Google images search is now supported in the Custom Search Engine API. See the API parameters section of this page. I'm using the API with python and for my application I just specify the parameter in the API call.
自定义搜索引擎 API 现在支持 Google 图片搜索。请参阅本页的 API 参数部分。我在 python 中使用 API,对于我的应用程序,我只在 API 调用中指定参数。
searchType = "image"
See this post on the cse blog.
请参阅cse 博客上的这篇文章。
EDIT: As Marc points out in his comment below, you need to click "Enable image search" in your CSE console.
编辑:正如 Marc 在下面的评论中指出的那样,您需要在 CSE 控制台中单击“启用图像搜索”。
回答by Paul Kane
Per the Google Custom Search Element Control API - documentation web site, this is possible.
根据 Google 自定义搜索元素控制 API - 文档网站,这是可能的。
https://developers.google.com/custom-search/docs/element
https://developers.google.com/custom-search/docs/element
This is the fragment used for searching by image by default:
这是默认用于按图像搜索的片段:
'defaultToImageSearch'
So I believe the full syntax for using this would be:
所以我相信使用它的完整语法是:
<script>
.
// Google custom search code, ids go here...
.
</script>
<gcse:search></gcse:search>
**<gcse:searchresults enableImageSearch="true" defaultToImageSearch="true">**
回答by user10000000
For those going through the WebExtensions tutorial, here's the updated code I used in popup.js to make it work with the new CSE functionality:
对于那些浏览 WebExtensions 教程的人,这里是我在 popup.js 中使用的更新代码,以使其与新的 CSE 功能一起使用:
/**
* @param {string} searchTerm - Search term for Google Image search.
* @param {function(string,number,number)} callback - Called when an image has
* been found. The callback gets the URL, width and height of the image.
* @param {function(string)} errorCallback - Called when the image is not found.
* The callback gets a string that describes the failure reason.
*/
function getImageUrl(searchTerm, callback, errorCallback) {
// Google image search - 100 searches per day.
// https://developers.google.com/image-search/
// var searchUrl = 'https://ajax.googleapis.com/ajax/services/search/images' +
// '?v=1.0&q=' + encodeURIComponent(searchTerm);
var searchUrl = 'https://www.googleapis.com/customsearch/v1' +
'?key=' + key + '&cx=' + cx + '&searchType=image&q=' + encodeURIComponent(searchTerm);
var x = new XMLHttpRequest();
x.open('GET', searchUrl);
// The Google image search API responds with JSON, so let Chrome parse it.
x.responseType = 'json';
x.onload = function() {
// Parse and process the response from Google Image Search.
var response = x.response;
if (!response || !response.items || response.items.length === 0) {
errorCallback('No response from Google Image search!');
return;
}
var firstResult = response.items[0];
// Take the thumbnail instead of the full image to get an approximately
// consistent image size.
var imageUrl = firstResult.image.thumbnailLink;
var width = parseInt(firstResult.image.thumbnailWidth);
var height = parseInt(firstResult.image.thumbnailHeight);
console.assert(
typeof imageUrl == 'string' && !isNaN(width) && !isNaN(height),
'Unexpected respose from the Google Image Search API!');
callback(imageUrl, width, height);
};
x.onerror = function() {
errorCallback('Network error.');
};
x.send();
}
Mainly it's changing the search URL (which should have searchType=image
as mentioned) and the response structural references in getImageUrl, and setting up the CSE engine. Make sure your CSE has Image search
turned on, and under Sites to search
make sure to select Search the entire web but emphasize included sites
from the options list.
主要是更改搜索 URL(应该有searchType=image
提到)和 getImageUrl 中的响应结构引用,并设置 CSE 引擎。确保您的 CSE 已Image search
打开,并Sites to search
确保Search the entire web but emphasize included sites
从选项列表中进行选择。
回答by Kris Craig
I'm not 100% certain on this, but I don't think the API supports what you're trying to do. This is not at all surprising, as Google's search API's are infamous for being lacking in even basic functionality (such as the standard search API's limit of 20 results, etc). I think the fact that I'm the first person to answer this in the 3 days it's been active is another indication that this is probably just not supported (or, if it is, Google never bothered to tell anyone).
我不是 100% 肯定这一点,但我认为 API 不支持你想要做的事情。这并不奇怪,因为 Google 的搜索 API 因缺乏基本功能而臭名昭著(例如标准搜索 API 的 20 个结果限制等)。我认为我是在它活跃的 3 天内第一个回答这个问题的事实是另一个迹象表明这可能只是不被支持(或者,如果是,谷歌从不费心告诉任何人)。
I know you're not going to like this, but I think your best option is to scrape the images out of the returned result set yourself. That's typically what people have to resort to when dealing with Google results data. Fortunately, their frontend code is remarkably consistent, so a few well-tuned regex matches and/or splits should do the trick for ya.
我知道你不会喜欢这个,但我认为你最好的选择是自己从返回的结果集中刮取图像。这通常是人们在处理 Google 结果数据时必须诉诸的。幸运的是,他们的前端代码非常一致,因此一些调整良好的正则表达式匹配和/或拆分应该可以为您提供帮助。
And yes, it's total BS that Google has provided such lousy support for this API. =)
是的,谷歌为这个 API 提供了如此糟糕的支持完全是愚蠢的。=)
回答by sibidiba
I tried to get a more authoritative answer in the official Google AJAX APIs group, and it seems the answer is NO(!). Google custom search API currently does not support image search only. You can use the deprecated Google image search API instead.
我试图在官方的 Google AJAX APIs group 中得到一个更权威的答案,似乎答案是否定的(!)。Google 自定义搜索 API 目前不只支持图片搜索。您可以改用已弃用的 Google 图片搜索 API。
check this
检查这个
回答by migueltonic
Try adding this line:
尝试添加这一行:
customSearchOptions['disableWebSearch'] = true;
回答by msroot
Try this one
试试这个
customSearchOptions['searchType'] = "image"
customSearchOptions['enableImageSearch'] = true
customSearchOptions['disableWebSearch'] = true;