小编典典

Python中的HTTP请求和JSON解析

json

我想通过Google Directions API动态查询Google
Maps。例如,此请求计算通过伊利诺伊州芝加哥市到密苏里州乔普林市和俄克拉荷马州俄克拉荷马市的两个航路点的路线:

http://maps.googleapis.com/maps/api/directions/json?origin =
Chicago,IL&destination = Los + Angeles,CA&waypoints = Joplin,MO
|俄克拉荷马州+市,OK&sensor =
false

以JSON格式返回结果。

如何在Python中做到这一点?我想发送这样的请求,接收结果并解析它。


阅读 234

收藏
2020-07-27

共1个答案

小编典典

我建议使用很棒的请求库:

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

2020-07-27