Ruby Google Custom Seach API从下一页获取结果 [英] Ruby Google Custom Seach API get results from next page
本文介绍了Ruby Google Custom Seach API从下一页获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码仅从Google分页的第一页获取结果...如何获得更多结果?
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
推荐答案
在下一个请求中仅将nextPageToken
用作参数,直到获得没有nextPageToken
键的结果
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'
}
)
这篇关于Ruby Google Custom Seach API从下一页获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文