【问题标题】:Get 'n' number of search results using google.customsearch('v1')使用 google.customsearch('v1') 获取“n”个搜索结果
【发布时间】:2015-11-03 04:25:20
【问题描述】:

默认情况下,它会给出 10 个结果。我怎样才能有n 的结果数?

到目前为止,这是我的代码(与他们的example 几乎相同):

var google = require('googleapis');
var customsearch = google.customsearch('v1');

const CX = '***';   // search engine ID
const API_KEY = '***';
const SEARCH = '***';

customsearch.cse.list({ cx: CX, q: SEARCH, auth: API_KEY }, function(err, resp) {
    if (err) {
        console.log('An error occured', err);
        return;
    }

    // Got the response from custom search
    console.log('Result: ' + resp.searchInformation.formattedTotalResults);

    if (resp.items && resp.items.length > 0) {
        var l = resp.items.length;
        console.log('# of results: ' + l); // this is always 10

        console.log('Results:', resp.items);
        for(var i=0; i<l; i++){
            console.log('Result # ' + (i+1) + ': ', resp.items[i]);
        }
    }
});

更新: 如果我想访问 238 结果怎么办?我知道我每次可以循环获得 10 个结果,但它不允许我超过 99 个结果。

【问题讨论】:

    标签: node.js google-custom-search google-api-nodejs-client


    【解决方案1】:

    您可以使用“num”选项。

    customsearch.cse.list({ cx: CX, q: SEARCH, auth: API_KEY, num: NUMBER }, function(err, resp) {
        ...
    });
    

    NUMBER 应该是介于 1 和 10 之间的整数。

    您可以在那里找到所有选项 - CSE: list

    【讨论】:

    • 我认为您没有理解我的问题。如果我想访问 238 个结果怎么办?我知道我每次可以循环并得到 10 个结果,但它不允许我超过 99 个结果。
    • 你可以使用numstart参数的组合
    猜你喜欢
    • 2013-04-06
    • 2016-07-03
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多