[英]Google Search API ruby
我正在使用rails 2.3。 我的要求是從Google搜索查詢獲得前20個鏈接。
我想使用http://rubygems.org/gems/google-api-client gem。
但我無法找到任何適當的文檔來進行自定義谷歌搜索與身份驗證。 任何人都能指出一個例子,我可以從中了解如何使用google-api-client gem進行自定義谷歌搜索?
如果有人偶然發現這個,這是此代碼的更新版本:
的Gemfile
gem 'google-api-client', '~> 0.11'
search.rb
require 'google/apis/customsearch_v1'
Search = Google::Apis::CustomsearchV1
search_client = Search::CustomsearchService.new
search_client.key = 'YOUR API KEY'
response = search_client.list_cses('your query', {cx: 'Search Engine ID'})
status, headers, body = response
pp status
pp headers
pp body
希望這可以幫助!
我看了一下API,看起來並不困難......只需要定義你想要使用哪個google的api 。 在您的情況下,您似乎想要使用customsearch api。
所以你的代碼應該是這樣的:
require 'google/api_client'
my_search_client = Google::APIClient.new
google_search = my_search_client.discovered_api('customsearch')
response = my_search_client.execute(
google_search.cse.list, 'q' => 'your query'
)
status, headers, body = response
對於身份驗證部分,您應該能夠直接從此頁面進行復制和粘貼。 只需使用自己的價值觀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.