【问题标题】:Google Places API, How to get next 20 results? [duplicate]Google Places API,如何获得下 20 个结果? [复制]
【发布时间】:2021-05-17 17:21:33
【问题描述】:

这是我第一次使用 Google Places API。 我正在用 Python 做一个练习,我试图从一个设定点获得 20 公里半径内的所有商店。

我能够毫无问题地获得前 20 个结果,但我不确定如何获得接下来的 20 个结果。 根据我在文档中阅读的内容,我需要使用“next_page_token”,但我无法成功。

这是我的测试代码:

url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=19.4196301,-99.16895&radius=20000&type=store&key=" + api_key

response = requests.get(url).json()

if response and response.get("status") == "REQUEST_DENIED":
    print(response)
    sys.exit(1)
elif response and response.get("status") != "ZERO_RESULTS":
    print(json.dumps(response, indent=4, sort_keys=True))
    print(len(response["results"]))
    print(">>>> PAGE 1")
    for result in response["results"]:
         print("Store name: ", result["name"])

    if "next_page_token" in response:
        print(">>>> PAGE 2")
        page2_token = response["next_page_token"]        
        url_page_2 = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=19.4196301,-99.16895&radius=20000&type=store&pagetoken=" + page2_token + "&key=" + api_key
        print(url_page_2)
        response_page_2 = requests.get(url_page_2).json()
        print(response_page_2)
        for result in response_page_2["results"]:
                print("Store name: ", result["name"])
    else:
        print("no next page")

当我的代码尝试执行第二页的请求时,我不断收到以下错误消息:

{'html_attributions': [], 'results': [], 'status': 'INVALID_REQUEST'}

我不确定在格式化第二页请求时我做错了什么(甚至我做错了)。有什么想法吗?

【问题讨论】:

标签: python google-places-api google-places


【解决方案1】:

对于下一页结果,请尝试仅在 url 中设置 pagetoken 参数。删除所有其他参数。

pagetoken — 从以前运行的搜索中返回最多 20 个结果。设置 pagetoken 参数将使用之前使用的相同参数执行搜索——除了 pagetoken 之外的所有参数都将被忽略。

【讨论】:

  • 这是正确的,它可以工作......但是它不能解决问题。我仍然遇到同样的错误。
  • 我刚试了一下。仅使用 pagetokenkey 参数。试试看,它对我有用。使用与您相同的参数。也尝试检查网址。
【解决方案2】:

刚刚找到解决方案!当您获得下一页令牌时,您需要等待几秒钟才能获得下一页。如果您立即请求,不知何故令牌信息尚未在谷歌服务器中。在请求之间添加一个简单的 time.sleep(2) 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2012-03-25
    • 2011-10-21
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多