【发布时间】:2018-05-28 21:38:05
【问题描述】:
我正在尝试使用对 Google Places API 的 python 请求在给定半径内的某个位置获取所有类型的餐厅。这是我的简单代码:
import requests
import json
APIKEY = "AIzaSyBFx8hqftDOlrSWRTiOSowjwfeS1OQtBpw"
def findPlaces(loc=("51.1079","17.0385"),radius=3000, pagetoken = None):
lat, lng = loc
type = "restaurant"
url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lng}&radius={radius}&type={type}&key={APIKEY}{pagetoken}".format(lat = lat, lng = lng, radius = radius, type = type,APIKEY = APIKEY, pagetoken = "&pagetoken="+pagetoken if pagetoken else "")
print(url)
response = requests.get(url)
res = json.loads(response.text)
print(res)
for result in res["results"]:
info = ";".join(map(str,[result["name"],result["geometry"]["location"]["lat"],result["geometry"]["location"]["lng"],result.get("rating",0),result["place_id"]]))
print(info)
pagetoken = res.get("next_page_token",None)
return pagetoken
pagetoken = None
while True:
pagetoken = findPlaces(pagetoken=pagetoken)
if not pagetoken:
break
但是,即使它在结果的第一页上运行良好,但在没有明显原因的情况下无法进入第二页(它只是从 API 返回 INVALID REQUEST)。此外,有趣的是,由于我将我的 url 打印到控制台,我只需单击它就可以跟踪这个非常具体的 url,当我这样做时,它运行良好,API 返回所需的餐馆列表。
我认为这是 url 编码字符串的问题,我用 python 的 urlencode 替换了格式,但是 - 结果保持不变。由于我的想法已经用完了,我想问:有人与这样的问题有关吗?
提前感谢您的帮助。
【问题讨论】:
标签: python request google-places-api