Menu
CoddyTech

显示合适的结果

Coddy Python 中的 API 课程的第 10 课,共 10 课。

challenge icon

挑战

中等

现在,你已经成功向 API 服务器发送了 GET 请求。接下来,我们通过访问嵌套字典中的值,显示想要的具体结果。

说明:

  1. 向以下地址发送 GET 请求:https://wttr.in/London?format=j1
  2. 将 requests.get() 对象保存到名为 response 的变量中
  3. 将 response.json() 保存到名为 data 的变量中
  4. 访问嵌套在 data["current_condition"][0] 中的当前天气状况,并将其保存到名为 current 的变量中
  5. 将 current["temp_C"] 保存到名为 temperature 的变量中
  6. 将 current["windspeedKmph"] 保存到名为 wind 的变量中
  7. 创建一个名为 city 的变量,并将其设为 "London"
  8. 在一条格式化消息中打印这三个变量

亲手试试

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 在线编译器