【问题标题】:Google Custom Search API start=100 causes error 400Google Custom Search API start=100 导致错误 400
【发布时间】:2017-12-04 11:41:10
【问题描述】:

我有一个使用 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 自定义搜索吗?

【问题讨论】:

    标签: php google-custom-search


    【解决方案1】:

    这个信息一点都不容易找到,我发现它只在 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(与您看到的 results 限制相反)有据可查,并且有关于如何绕过它的信息(当然是通过注册计费)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2020-07-29
      • 2018-01-15
      相关资源
      最近更新 更多