【问题标题】:Using Google Custom Search Engine through the google-api-client gem通过 google-api-client gem 使用 Google 自定义搜索引擎
【发布时间】: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


【解决方案1】:

来自 API Explorer:https://developers.google.com/apis-explorer/#p/customsearch/v1/search.cse.list

看起来您要查找的参数是“开始”:要返回的第一个结果的索引(整数)。另一个参数(num)是返回的结果个数。

您可以在 Explorer 上手动尝试,看看为什么它没有返回您需要的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    相关资源
    最近更新 更多