Ruby Google Custom Seach API get results from next page

微笑、不失礼 提交于 2019-12-13 17:26:21

问题


the code below is only getting the results from the first page of the google pagination... how to get more results?

require 'google/api_client'

# autenticação
client = Google::APIClient.new(:application_name => 'Ruby Drive sample',
      :application_version => '1.0.0', 
      :client_id => 'xxxxxxxxxxxxxxxx',
      :client_secret => 'xxxxxxxxxxxxxx',
      :authorization => nil)
search = client.discovered_api('customsearch')

# chama a API
response = client.execute(
  :api_method => search.cse.list,
    :parameters => {
      'q' => 'cafe',
      'key' => 'xxxxxxxxxxxx',
      'cx' => 'xxxxxxxxxxxxxxxx'
    }
)

# recebe a resposta em json
body = JSON.parse(response.body)
items = body['items']

# printa algumas informações...
items.each do |item|
  puts "#{item['formattedUrl']}"
end

回答1:


Simply use the nextPageToken as a parameter in the next request until you get back a result without a nextPageToken key

next_page_token = body['nextPageToken']

response_page2 = client.execute(
  :api_method => search.cse.list,
  :parameters => {
    'nextPageToken' => next_page_token,
    'key' => 'xxxxxxxxxxxx',
    'cx' => 'xxxxxxxxxxxxxxxx'
  }
)


来源:https://stackoverflow.com/questions/24602642/ruby-google-custom-seach-api-get-results-from-next-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!