显示合适的结果
Coddy Python 中的 API 课程的第 10 课,共 10 课。
挑战
中等现在,你已经成功向 API 服务器发送了 GET 请求。接下来,我们通过访问嵌套字典中的值,显示想要的具体结果。
说明:
- 向以下地址发送 GET 请求:
https://wttr.in/London?format=j1 - 将
requests.get()对象保存到名为response的变量中 - 将
response.json()保存到名为data的变量中 - 访问嵌套在
data["current_condition"][0]中的当前天气状况,并将其保存到名为current的变量中 - 将
current["temp_C"]保存到名为temperature的变量中 - 将
current["windspeedKmph"]保存到名为wind的变量中 - 创建一个名为
city的变量,并将其设为"London" - 在一条格式化消息中打印这三个变量
亲手试试
import requests
# Step 1: Send GET request
response = # Your code here
# Step 2: Parse JSON response
data = # Your code here
# Step 3: Extract specific data
temperature = # Your code here
wind = # Your code here
city = # Your code here
# Step 4: Display results
print(f"City: {city}")
print(f"Temperature: {temperature}")
print(f"Wind: {wind}")Python 中的 API中的所有课
自己练习: Python 在线编译器