Menu
Coddy logo textTech

Displaying Appropriate Results

Coddyの「Pythonで学ぶAPI」コースのレッスン 10/10。

challenge icon

チャレンジ

中級

APIサーバーへのGETリクエストの送信に成功しました。次は、ネストされた辞書の値にアクセスして、目的の特定の結果を表示してみましょう。

手順:

  1. https://wttr.in/London?format=j1 にGETリクエストを送信します
  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. 3つの変数すべてをフォーマットされたメッセージで出力します

自分で試してみよう

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のすべてのレッスン