开发者

Image suggestion with jQuery from Google

开发者 https://www.devze.com 2022-12-27 12:19 出处:网络
I\'d like to make a script which search in Go开发者_开发技巧ogle and shows the 1st relevant image.

I'd like to make a script which search in Go开发者_开发技巧ogle and shows the 1st relevant image.

I mean, if you type to the input field: car

if you search to word "car" via Google and it suggest the 1st image for me.

What do you think, are there any method to do this?


Yes, this is perfectly possible using the Google AJAX Search API.

You code would look something like this:

var imageSearch = new google.search.ImageSearch();
imageSearch.setSearchCompleteCallback(this, function(results) {
  // figure out which picture from results you want
});
imageSearch.execute('car');


You would need either a JSON-P API from Google that provides that data (and I don't believe they provide one) or to write a server side script that runs on your server and queries Google (which might be a violation of their Terms of Service, so check the small print before going any further).

Update:

5.3 You agree not to access (or attempt to access) any of the Services by any means other than through the interface that is provided by Google, unless you have been specifically allowed to do so in a separate agreement with Google. You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers) and shall ensure that you comply with the instructions set out in any robots.txt file present on the Services.

What you are asking to do is accessing the data through automated means, so you would be violating the Google terms of service.


yes there's an api.... check this:

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

you can always play around: http :// code.google.com/apis/ajax/playground/

/*************************************************************************************************************************************************************************************************************************** As long as you have been specifically allowed to do so in a separate agreement with Google. ************************************************************************************************************************************************************************************************************************/


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号