LoginSignup
47
47

More than 5 years have passed since last update.

とりあえずGoogle Custom Searchで画像を取得してみる

Last updated at Posted at 2015-02-18

とりあえずGoogle Custom Searchで画像を取得してみる

最近Ajax使ってJSONを取ってきていろいろしてみる機会があったので、公開されているAPI使って似たようなことが出来ないか試してみた。

Custom Search JSON/Atom API

GET https://www.googleapis.com/customsearch/v1

このアドレスにパラメータをつければ取得できる。
Atomも対応してるみたいだけど、特に指定しなければ標準でJSON形式で取得できるっぽい。

Parameter Meaning Notes
alt Data format for the response. Valid values: json, atom
Default value: json

API Keyと検索エンジンIDの準備

google custom search engine(CSE)を使って、検索結果をjsonで取得する
取得方法はこちらの記事に非常にわかりやすくまとまってます。
ありがとうございます。

画像検索するためには、Custom Search Engineで作成した検索エンジン内で
設定 -> 基本 の「画像検索」をオンにするのを忘れずに。

取得してみる

https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={SEARCH_ENGINE_ID}&searchType=image&q={SEARCH_WORDS}

API Keyはkey=のあとに。
検索エンジンIDはcx=のあとに。
検索したい語句はq=のあとにそれぞれ指定。

上記のようにsearchType=にimageを設定してやれば画像検索ができる。

Reference

https://developers.google.com/custom-search/json-api/v1/reference/cse/list
他のパラメータはここにいろいろ載ってます。


items以下に配列で返ってくるっぽい。
画像検索の場合はlinkが該当画像URLかな?

 "items": [
  {
   "kind": "customsearch#result",
   "title": "猫好き集まれ!癒しのねこ画像まとめ - NAVER まとめ",
   "htmlTitle": "\u003cb\u003e猫\u003c/b\u003e好き集まれ!癒しの\u003cb\u003eねこ\u003c/b\u003e画像まとめ - NAVER まとめ",
   "link": "http://bluemark.info/wp-content/uploads/2013/02/3a4465fcdc9a8bb92e40ac1456d52d6f.jpg",
   "displayLink": "matome.naver.jp",
   "snippet": "出典bluemark.info",
   "htmlSnippet": "出典bluemark.info",
   "mime": "image/jpeg",
   "image": {
    "contextLink": "http://matome.naver.jp/odai/2139450632745180701",
    "height": 3739,
    "width": 4013,
    "byteSize": 3884076,
    "thumbnailLink": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRlvyqhlNV9IrNmRPBoToEYhEzdxYBHHWHeSEIV3_I9d_cnNsGeETqtsxAU",
    "thumbnailHeight": 140,
    "thumbnailWidth": 150
   }
  }

あとはええがに。

47
47
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
47