Google Search API会因有效参数而引发错误

时间:2016-06-10 15:01:58

标签: google-search google-custom-search google-search-api

我正在使用Google.Apis.Customsearch.v1进行自定义Google搜索。该查询采用numstart参数进行分页。根据Google startnum查询参数的文档here

开始

  

可选。 start参数指示应包含在搜索结果中的第一个匹配结果。 start参数使用从零开始的索引,这意味着第一个结果为0,第二个结果为1,依此类推。

     

start参数与num参数一起使用以确定要返回的搜索结果。请注意,任何查询都不会返回超过1000个结果,即使超过1000个文档与查询匹配,因此将start设置为1000或更多将不会产生任何结果。

num

  

描述
  可选的。 num参数标识要返回的搜索结果的数量。

     

默认的num值是10,最大值是20.如果您请求超过20个结果,则只返回20个结果。

     

注意:如果搜索结果总数少于请求的结果数,则会返回所有可用的搜索结果。

但是,当我将start设置为0并将num设置为20时,我会收到错误

  

{" Google.Apis.Requests.RequestError \ r \ n无效值[400] \ r \ n错误[\ r \ n \ tMessage [无效值]位置[ - ]原因[无效]域[全局] \ r \ n]的\ r \ n"}

这是我的代码

        CustomsearchService customSearchService =
            new CustomsearchService(new Google.Apis.Services.BaseClientService.Initializer() { ApiKey = apiKey });
    Google.Apis.Customsearch.v1.CseResource.ListRequest listRequest = customSearchService.Cse.List(query);
    listRequest.Cx = searchengineid;            
    listRequest.Start = 0;
    listRequest.Num = 20;
    Search search = listRequest.Execute();

我必须将start设置为1并将num设置为10才能使其正常工作。那么为什么它不按照文档

工作

1 个答案:

答案 0 :(得分:0)

您引用的资源是XML API引用:

  

此页面引用自定义搜索API的XML版本,即   仅适用于Google Site Search客户。

我认为您使用的库是JSON/Atom API

https://developers.google.com/custom-search/json-api/v1/reference/cse/list

据我所知,零不是start参数的有效值,num参数在JSON / Atom API中略有不同。

  

num无符号整数) - 要返回的搜索结果数。有效值为1到10之间的整数。

https://developers.google.com/custom-search/json-api/v1/reference/cse/list#num