26

由于不推荐使用Google 图片搜索 API,因此应该使用Google 自定义搜索 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 文档很差,它只描述了如何添加额外的结果。

4

7 回答 7

36

自定义搜索引擎 API 现在支持 Google 图片搜索。请参阅本页的 API 参数部分。我将 API 与 python 一起使用,对于我的应用程序,我只需在 API 调用中指定参数。

searchType = "image"

请参阅cse 博客上的这篇文章

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

于 2012-10-02T19:26:00.880 回答
6

根据 Google Custom Search Element Control 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">**
于 2013-01-26T17:14:44.743 回答
5

对于那些正在阅读 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();
}

主要是在getImageUrl中更改搜索URL(应该有searchType=image)和响应结构引用,并设置CSE引擎。确保您的 CSE 已Image search打开,并Sites to search确保Search the entire web but emphasize included sites从选项列表中进行选择。

于 2017-04-08T18:04:13.493 回答
3

我对此不是 100% 确定的,但我认为 API 不支持您正在尝试做的事情。这一点也不奇怪,因为 Google 的搜索 API 因缺乏基本功能而臭名昭著(例如标准搜索 API 的 20 个结果限制等)。我认为我是第一个在它活跃的 3 天内回答这个问题的人,这表明这可能只是不支持(或者,如果是,谷歌从来没有费心告诉任何人)。

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

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

于 2011-12-12T22:33:11.550 回答
2

Try adding this line:

customSearchOptions['disableWebSearch'] = true;
于 2012-06-04T16:20:23.263 回答
2

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

检查这个

于 2011-12-14T10:50:09.260 回答
2

试试这个

customSearchOptions['searchType'] = "image"
customSearchOptions['enableImageSearch'] = true
customSearchOptions['disableWebSearch'] = true;
于 2015-06-04T14:46:35.740 回答