我想通过 Google Directions API 动态查询 Google map 。例如,此请求计算从伊利诺伊州芝加哥到加利福尼亚州洛杉矶的路线,途经位于密苏里州乔普林和俄克拉荷马州俄克拉荷马市的两个航路点:
http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false
返回结果in the JSON format .
如何在 Python 中做到这一点?我想发送这样一个请求,接收结果并解析它。
最佳答案
我推荐使用很棒的 requests图书馆:
import requests
url = 'http://maps.googleapis.com/maps/api/directions/json'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below
JSON 响应内容:https://requests.readthedocs.io/en/master/user/quickstart/#json-response-content
https://stackoverflow.com/questions/6386308/