向服务器发送 GET 请求
Coddy Python 中的 API 课程的第 9 课,共 10 课。
挑战
中等现在你已经与 API 建立了正确的连接,接下来发送一个 GET 请求来查询特定地点。
- 发送 GET 请求到:
https://wttr.in/London?format=j1(?format=j1部分会告诉服务返回 JSON) - 将
requests.get()对象保存到名为response的变量中 - 使用
response.json()解析响应,并将结果保存到名为data的变量中 - 当前天气状况位于
data["current_condition"][0]中。提取温度(temp_C)和天气描述(weatherDesc[0]["value"]),然后将两者都打印出来
亲手试试
import requests
# Step 1: Send GET request for London weather (j1 = JSON format)
response = # Your code here
# Step 2: Parse JSON response
data = # Your code here
# Step 3: Print weather information
current = data["current_condition"][0]
print("Temperature:", current["temp_C"], "°C")
print("Description:", current["weatherDesc"][0]["value"])Python 中的 API中的所有课
自己练习: Python 在线编译器