2010-04-15 26 views

回答

1

疑难杂症

<!-- 
    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我们热爱谷歌。

+0

:) – 2010-04-16 05:14:16

1

您需要无论是从谷歌一个JSON-P API,它提供的数据(我不相信他们提供一个),或写你的服务器上运行,并查询谷歌(服务器端脚本威力违反他们的服务条款,因此请在继续之前检查small print)。

更新:

5.3您同意不访问(或试图访问)任何通过由谷歌提供的,除非你已经明确允许这样做比通过接口以外的任何手段服务所以在与谷歌单独的协议。您特别同意不通过任何自动方式(包括使用脚本或网络抓取工具)访问(或尝试访问)任何服务,并确保您遵守服务中存在的任何robots.txt文件中列出的说明。

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

+0

是的,不是。 :)我给他们发了一条消息。我希望他们能给我一个。 (因为我忠于Google):P – 2010-04-15 06:06:30

+0

@Zoltan,@David,这是不正确的,这里有一个API。看到我的答案。 – Aistina 2010-04-15 09:19:27

1

是有一个API ....检查:

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

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

/************************************************ ************************************************** ************************************************** ************************************************** ********************* 只要您特别允许您在与Google单独签署的协议中这样做。 ************************************************* ************************************************** ************************************************** ************************************************** *****************/

+1

这是一个违反了Google服务条款第5.3节的屏幕截图工具。为需要它的每个人提供 – Quentin 2010-04-15 09:51:59

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');