Google Maps Services Places 的 Python 客户端无法传递 Page_Token

标签 python google-maps

我正在试用 Google map 服务的 Python 客户端,以使用 Places API 提取地点列表。

这是 GitHub 页面:https://github.com/googlemaps/google-maps-services-python 这是文档页面:https://googlemaps.github.io/google-maps-services-python/docs/2.4.4/#module-googlemaps.exceptions

在 .places def 中,我需要以字符串格式输入 page_token 以获得接下来的 10 个列表。当我运行下面的代码时,它一直显示 INVALID_REQUEST

这是我的代码:

places_result = gmaps.places(query="hotels in Singapore", page_token='')
for result in places_result['results']:
    print(result['name'])
try :
places_result = gmaps.places(query="hotels in Singapore", page_token=places_result['next_page_token'])
except ApiError as e:
    print(e)
else:
    for result in places_result['results']:
    print(result['name'])

最佳答案

好的,经过数小时的反复试验。我注意到我需要添加一个 time.sleep(2) 才能使其正常工作。我不确定为什么,但它有效。

用time.sleep(1)失败,time.sleep(2)及以上版本将解决问题。

希望有人能阐明背后的原因。

这是我的代码,用于检索地点并转到下一页直到结束。记得更换 1. 您在“YOUR KEY HERE”处的 key 和 2. 在'SEARCH SOMETHING' 中要搜索的字符串。

import googlemaps
import time

gmaps = googlemaps.Client(key='YOUR KEY HERE')

def printHotels(searchString, next=''):
    try:
        places_result = gmaps.places(query=searchString, page_token=next)
    except ApiError as e:
        print(e)
    else:
        for result in places_result['results']:
            print(result['name'])
    time.sleep(2)
    try:
        places_result['next_page_token']
    except KeyError as e:
        print('Complete')
    else:
        printHotels(searchString, next=places_result['next_page_token'])

if __name__ == '__main__':
    printHotels('SEARCH SOMETHING')

关于Google Maps Services Places 的 Python 客户端无法传递 Page_Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39395524/

相关文章:

java - 移动后端启动器 subscribeToCloudMessage 将不起作用

python - pandas ValueError : numpy. dtype 大小错误,尝试重新编译

python - Web.py:无法通过多个浏览器选项卡获得多线程行为

python - 当作为函数调用时,向 easy_install 添加额外的查找链接的正确方法是什么?

python - 将 Python Statsmodel ARIMA 模型参数重新调整为新数据并进行预测

android - 显示异常的地方自动完成 api

api - 从当前位置到标记的 Google map API 路线

javascript - 如何在 php 中添加纬度和经度编码,因为它显示 map 图标,但如果我们单击它会出现错误的 URL

python - 这是上下文管理器的有效用例吗?

javascript - 多条谷歌地图折线和信息窗口