LoginSignup
1
0

More than 1 year has passed since last update.

Ruby から Google Custom Search API を叩く

Posted at

ゴール

以下を Ruby から叩く

まず REST API を呼ぶまで

Gem から API を叩く

require 'google/apis/customsearch_v1'

# Doc: https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/CustomsearchV1/CustomSearchAPIService.html
searcher = Google::Apis::CustomsearchV1::CustomSearchAPIService.new
searcher.key = 'APIキー'

# Doc: https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/CustomsearchV1/Search.html
results = searcher.list_cses(cx: '検索エンジンID', q: '検索内容')

# Doc: https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/CustomsearchV1/Search/SearchInformation.html
results.search_information.total_results

# Doc: https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/CustomsearchV1/Result.html
items = results.items
items.first.title
items.first.snippet
items.first.link
# Doc: https://developers.google.com/custom-search/docs/structured_data#pagemaps
items.first.pagemap['cse_image'].first['src']
1
0
0

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
1
0