2014-02-18 106 views
1

我正在嘗試使用Google自定義搜索引擎API進行一個相當基本的搜索。 我現在面臨的問題是,唯一的結果我顯然能夠得到的是第10:通過google-api-client gem使用Google自定義搜索引擎

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 

此代碼是很簡單,但工作得很好:首先我初始化用我的GCS客戶端我API_KEY,然後我就可以用我喜歡的任何參數調用它,例如:

client.query('poker', 10) 

搜索我的引擎的自定義以字符串「撲克」,並從結果集的第10個元素開始。我的問題是,它不起作用,我總是得到相同的結果,我會得到沒有startIndex選項。問題可能是我不知道該參數是否以這種方式命名,或者如果它是我可以使用的正確的參數:我試過startIndex,start,start_index,num(這個可以工作,但最大值是10,並且我至少需要30個結果),有時參數會被拒絕,有時候不會產生任何效果。

有沒有人做過這樣的事情,並可以幫助我?

+0

我只是在這裏猜測 - 也許選項名稱應該是'start_index'而不是'startIndex'? –

回答

相關問題