Google自定义搜索图片

时间:2011-12-09 16:44:55

标签: javascript google-search-api google-image-search

由于不推荐使用Google image search API,因此应该使用Google custom search API

我用它做了一个小例子。我的问题是我想要返回谷歌图片搜索结果。由此显示网络结果,用户可以切换到图像结果。 默认情况下,如何仅显示图像效果?

<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" />

API文档很差,它只描述了如何添加其他结果。

7 个答案:

答案 0 :(得分:29)

自定义搜索引擎API现在支持Google图片搜索。请参阅this page的API参数部分。我正在使用带有python的API,对于我的应用程序,我只是在API调用中指定参数。

searchType = "image"

请参阅this post on the cse blog

编辑:正如Marc在下面的评论中指出的那样,您需要在CSE控制台中点击“启用图像搜索”。

答案 1 :(得分:4)

根据Google自定义搜索元素控制API - 文档网站,这是可能的。

https://developers.google.com/custom-search/docs/element

默认情况下,这是用于按图搜索的片段:

'defaultToImageSearch'

所以我相信使用它的完整语法是:

<script>
.
// Google custom search code, ids go here...
.
</script>
<gcse:search></gcse:search>
**<gcse:searchresults enableImageSearch="true" defaultToImageSearch="true">**

答案 2 :(得分:3)

我对此并不是100%肯定,但我不认为API支持您尝试做的事情。这并不奇怪,因为Google的搜索API因缺乏基本功能(例如标准搜索API的20个结果限制等)而臭名昭着。我认为我是第一个在活跃的3天内回答这个问题的人这一事实再次表明这可能不受支持(或者,如果是这样,谷歌从不打扰告诉任何人)。

我知道你不会喜欢这个,但我认为你最好的选择是自己从返回的结果集中删除图像。这通常是人们在处理Google结果数据时必须采用的方式。幸运的是,他们的前端代码非常一致,因此一些经过良好调整的正则表达式匹配和/或拆分应该可以解决问题。

是的,谷歌已经为这个API提供了如此糟糕的支持。 =)

答案 3 :(得分:3)

对于那些通过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();
}

主要是改变搜索URL(应该提到searchType=image)和getImageUrl中的响应结构引用,以及设置CSE引擎。确保您的自定义搜索引擎已启用Image search,并在Sites to search下确保从选项列表中选择Search the entire web but emphasize included sites

答案 4 :(得分:2)

我试图在官方Google AJAX API小组中获得更权威的答案, 似乎答案是否定的(!)。 Google自定义搜索API目前不支持图片搜索。您可以改用已弃用的Google图片搜索API。

检查this

答案 5 :(得分:2)

尝试添加此行:

customSearchOptions['disableWebSearch'] = true;

答案 6 :(得分:1)

试试这个

customSearchOptions['searchType'] = "image"
customSearchOptions['enableImageSearch'] = true
customSearchOptions['disableWebSearch'] = true;