1

我想制作一个在 Google 中搜索并显示第一张相关图片的脚本。

我的意思是,如果您在输入字段中键入:汽车

如果您通过 Google 搜索“汽车”一词,它会为我推荐第一张图片。

你怎么看,有什么方法可以做到这一点?

4

4 回答 4

1

gotcha

<!--
  copyright (c) 2009 Google inc.

  You are free to copy and use this sample.
  License can be found here: http://code.google.com/apis/ajaxsearch/faq/#license
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google AJAX Search API Sample</title>
    <script src="http://www.google.com/jsapi?key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q"></script>
    <script type="text/javascript">
    /*
    *  How to search for images and restrict them by size.
    *  This demo will also show how to use Raw Searchers, aka a searcher that is
    *  not attached to a SearchControl.  Thus, we will handle and draw the results
    *  manually.
    */

    google.load('search', '1');

    function searchComplete(searcher) {
      // Check that we got results
      if (searcher.results && searcher.results.length > 0) {
        // Grab our content div, clear it.
        var contentDiv = document.getElementById('content');
        contentDiv.innerHTML = '';

        // Loop through our results, printing them to the page.
        var results = searcher.results;
        for (var i = 0; i < results.length; i++) {
          // For each result write it's title and image to the screen
          var result = results[i];
          var imgContainer = document.createElement('div');

          var title = document.createElement('h2');
          // We use titleNoFormatting so that no HTML tags are left in the title
          title.innerHTML = result.titleNoFormatting;

          var newImg = document.createElement('img');
          // There is also a result.url property which has the escaped version
          newImg.src = result.tbUrl;

          imgContainer.appendChild(title);
          imgContainer.appendChild(newImg);

          // Put our title + image in the content
          contentDiv.appendChild(imgContainer);
        }
      }
    }

    function OnLoad() {
      // Our ImageSearch instance.
      var imageSearch = new google.search.ImageSearch();

      // Restrict to extra large images only
      imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE,
                                 google.search.ImageSearch.IMAGESIZE_MEDIUM);

      // Here we set a callback so that anytime a search is executed, it will call
      // the searchComplete function and pass it our ImageSearch searcher.
      // When a search completes, our ImageSearch object is automatically
      // populated with the results.
      imageSearch.setSearchCompleteCallback(this, searchComplete, [imageSearch]);

      // Find me a beautiful car.
      imageSearch.execute("Subaru STI");
    }
    google.setOnLoadCallback(OnLoad);
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="content">Loading...</div>
  </body>
</html>
​

parapapapa we love google.

于 2010-04-16T05:13:16.230 回答
1

是的,这完全可以使用Google AJAX Search API实现。

你的代码看起来像这样:

var imageSearch = new google.search.ImageSearch();
imageSearch.setSearchCompleteCallback(this, function(results) {
  // figure out which picture from results you want
});
imageSearch.execute('car');
于 2010-04-15T08:50:48.797 回答
1

您将需要来自 Google 的 JSON-P API 来提供该数据(我不相信他们提供)或编写在您的服务器上运行并查询 Google 的服务器端脚本(这可能违反他们的条款服务,所以在继续之前检查小字体)。

更新:

5.3 您同意不通过谷歌提供的界面以外的任何方式访问(或试图访问)任何服务,除非您在与谷歌的单独协议中被明确允许这样做。您明确同意不通过任何自动化方式(包括使用脚本或网络爬虫)访问(或尝试访问)任何服务,并应确保您遵守服务中存在的任何 robots.txt 文件中的说明.

您要求做的是通过自动方式访问数据,因此您将违反 Google 服务条款。

于 2010-04-15T05:43:45.937 回答
1

是的,有一个 api.... 检查这个:

http://www.codeproject.com/KB/IP/google_image_search_api.aspx

你可以随时玩耍:http://code.google.com/apis/ajax/playground/

/************************************************* ****************************************************** ****************************************************** ****************************************************** ******************** 只要您在与 Google 的单独协议中被明确允许这样做。****************************************************** ****************************************************** ****************************************************** ****************************************************** ******************/

于 2010-04-15T08:44:03.893 回答