【发布时间】:2014-02-18 15:07:18
【问题描述】:
我正在尝试使用 Google 自定义搜索引擎 API 完成一个相当基本的搜索。 我面临的问题是我显然能够得到的唯一结果是前 10 个:
module WalterSobchak
class GoogleCustomSearch
def initialize
@client = Google::APIClient.new(
key: configatron.google.api_key, authorization: nil)
@search = @client.discovered_api('customsearch')
end
def query(q, num)
@client.execute(api_method: @search.cse.list,
parameters: {q: q, startIndex: num,
key: configatron.google.api_key,
cx: configatron.google.custom_search_engine})
end
end
end
这段代码非常简单,但运行良好:首先我使用我的 api_key 初始化我的 GCS 客户端,然后我可以使用我喜欢的任何参数调用它,例如:
client.query('poker', 10)
使用字符串“poker”并从结果集的第 10 个元素开始搜索我的自定义引擎。我的问题是它不起作用,我总是得到与没有 startIndex 选项相同的结果。问题可能是我不知道该参数是否以这种方式命名,或者它是否是我可以使用的正确参数:我尝试过 startIndex、start、start_index、num(这个有效,但最大值为 10,并且我需要至少30个结果),有时参数被拒绝,有时它不会造成任何影响。
以前有没有人做过类似的事情并且可以帮助我?
【问题讨论】:
-
我只是在这里猜测 - 也许选项名称应该是
start_index而不是startIndex?
标签: ruby-on-rails ruby search google-api