gpt4 book ai didi

python - Python 中的 HTTP 请求和 JSON 解析

转载 作者:bug小助手 更新时间:2023-10-28 01:27:03 24 4
gpt4 key购买 nike

我想通过 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

关于python - Python 中的 HTTP 请求和 JSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6386308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com