Googleで検索し、最初の関連画像を表示するスクリプトを作成したいと思います。
つまり、入力フィールドに入力すると、car
Googleで「車」という単語を検索すると、最初の画像が表示されます。
あなたはどう思いますか、これを行う方法はありますか?
Googleで検索し、最初の関連画像を表示するスクリプトを作成したいと思います。
つまり、入力フィールドに入力すると、car
Googleで「車」という単語を検索すると、最初の画像が表示されます。
あなたはどう思いますか、これを行う方法はありますか?
はい、APIがあります...これを確認してください:
http://www.codeproject.com/KB/IP/google_image_search_api.aspx
いつでも遊ぶことができます:http:// code.google.com/apis/ajax/playground/
/ ************************************************* ************************************************** ************************************************** ************************************************** ******************** Googleとの別の契約で特に許可されている限り、************************************************** ************************************************** ************************************************** ************************************************** **************** /
そのデータを提供するGoogleのJSON-PAPIが必要になるか(データが提供されるとは思わない)、サーバー上で実行されてGoogleにクエリを実行するサーバーサイドスクリプトを作成する必要があります(これは利用規約に違反している可能性があります)。サービスの、それで先に進む前に小さな印刷物をチェックしてください)。
アップデート:
5.3ユーザーは、Googleとの別の契約で特に許可されている場合を除き、Googleが提供するインターフェース以外の方法でサービスにアクセスしない(またはアクセスを試みない)ことに同意するものとします。自動化された手段(スクリプトまたはWebクローラーの使用を含む)を介してサービスにアクセスしない(またはアクセスを試みない)ことに特に同意し、サービスに存在するrobots.txtファイルに記載されている指示に準拠するようにする必要があります。
あなたが求めているのは自動化された手段でデータにアクセスすることなので、Googleの利用規約に違反することになります。
はい、これはGoogle AJAXSearchAPIを使用して完全に可能です。
コードは次のようになります。
var imageSearch = new google.search.ImageSearch();
imageSearch.setSearchCompleteCallback(this, function(results) {
// figure out which picture from results you want
});
imageSearch.execute('car');
落とし穴
<!--
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>
パラパパパ私たちはグーグルが大好きです。