Google Custom Search API start = 100会导致错误400

时间:2017-12-04 11:41:10

标签: php google-custom-search

我有一个使用Google自定义搜索API的脚本,遍历多个结果页面。

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=0&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=10&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=20&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

在上述所有例子中,我得到了适当的回应。查询响应声称搜索结果有17,900个。 但是当脚本达到start = 100时:

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=100&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

我收到以下响应(这是转换为PHP对象的JSON响应):

stdClass Object (
        [error] => stdClass Object
            (
                [errors] => Array
                    (
                        [0] => stdClass Object
                            (
                                [domain] => global
                                [reason] => invalid
                                [message] => Invalid Value
                            )
                    )
                [code] => 400
                [message] => Invalid Value
            ) )

尽管我在start = 90中收到的结果声称下一页存在:

"nextPage": [
   {
    "title": "Google Custom Search - \"bank\"",
    "totalResults": "17900",
    "searchTerms": "\"bank\"",
    "count": 10,
    "startIndex": 100,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "[[CX VALUE]]",
    "sort": "date",
    "googleHost": "www.google.com",
    "hl": "en"
   }
  ]

使用API​​显示,当start = 92时,此无效值错误会出现。 此外,这是每个关键字搜索出现此错误的确切页面。 任何有关该问题的帮助将不胜感激。这是因为这是Google自定义搜索的免费版本吗?

1 个答案:

答案 0 :(得分:14)

这个信息根本不容易找到,我发现它只在google.com上的一个地方正式提到。我在the Custom Search JSON API docs中找到了一行,描述了nextPage响应元素:

  

注意:此API仅返回前100个结果。

没有提及仅限于免费API。我还发现用户报告确认即使您注册结算也适用此限制,例如,请参阅this related SO questionthe linked blog post

OTOH the 100 queries/day limit(与您所看到的结果限制相反)有详细记录,并且有关于如何绕过该地方的信息(当然,通过注册结算) )。