【发布时间】: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