我通过谷歌地图库发送了一个请求,我得到了 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() 是因为 unicode 和 googlemaps 中的“next_page_token”输出需要“str”。
知道我做错了什么吗?我该如何解决?
请您参考如下方法:
如果您再次传递所有参数(除了 page_token 之外),它应该可以工作。请注意,您需要 add a couple of seconds delay以允许在 Google 的服务器上验证 token 。
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']
