谷歌地图python next_page_token不起作用

时间:2017-11-19 10:46:11

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

我通过谷歌地图库发送请求,我得到了20个地方的结果。

我的代码看起来像那样

gmaps = googlemaps.Client(key='MY_KEY_IS_HERE')

x = gmaps.places(query=['restaurants', 'bakery', 'cafe', 'food'],location='40.758896, -73.985130',radius=10000)

print len(x['results']) # outputs 20 (which is maximum per 1 page result)

之后我想请求第二页,我想是正确使用它。

print gmaps.places(page_token=x['next_page_token'].encode())

并返回一个奇怪的错误

    File "/Users/././testz.py", line 15, in <module>
    print gmaps.places(page_token=x['next_page_token'].encode())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlemaps/client.py", line 356, in wrapper
    result = func(*args, **kwargs)
TypeError: places() takes at least 2 arguments (2 given)

我使用.encode(),因为&#39; next_page_token&#39; unicode和googlemaps中的输出需要&#39; str&#39;。

知道我做错了什么以及如何解决?

1 个答案:

答案 0 :(得分:1)

如果再次传递所有参数(除page_token之外),它应该有效。请注意,您需要add a couple of seconds delay才能在Google服务器上验证令牌。

import googlemaps
import time

k="your key here"

gmaps = googlemaps.Client(key=k)

params = {
    'query': ['restaurants', 'bakery', 'cafe', 'food'],
    'location': (40.758896, -73.985130),
    'radius': 10000
}

x = gmaps.places(**params)
print len(x['results']) # outputs 20 (which is maximum per 1 page result)
print x['results'][0]['name']

params['page_token'] = x['next_page_token']

time.sleep(2)
x1 = gmaps.places(**params)

print len(x1['results'])
print x1['results'][0]['name']