Google custom search for images only

sib*_*iba 19 javascript google-search-api google-image-search

Since Google image search API is deprecated, one should use Google custom search API for this.

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" />
Run Code Online (Sandbox Code Playgroud)

The API documentation is quite poor, it only describes how to add additional results.

kel*_*rek 29

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

searchType = "image"
Run Code Online (Sandbox Code Playgroud)

在cse博客上看到这篇文章.

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

  • 为此,您需要在CSE控制台中单击"启用图像搜索"! (8认同)
  • 我认为[this](https://developers.google.com/custom-search/json-api/v1/reference/cse/list)是您可以发送的参数的文档,包括`searchType`. (2认同)

Pau*_*ane 6

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

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

这是默认用于按图像搜索的片段:

'defaultToImageSearch'
Run Code Online (Sandbox Code Playgroud)

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

<script>
.
// Google custom search code, ids go here...
.
</script>
<gcse:search></gcse:search>
**<gcse:searchresults enableImageSearch="true" defaultToImageSearch="true">**
Run Code Online (Sandbox Code Playgroud)


use*_*000 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();
}
Run Code Online (Sandbox Code Playgroud)

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