Displaying Appropriate Results
Coddyの「Pythonで学ぶAPI」コースのレッスン 10/10。
チャレンジ
中級APIサーバーへのGETリクエストの送信に成功しました。次は、ネストされた辞書の値にアクセスして、目的の特定の結果を表示してみましょう。
手順:
https://wttr.in/London?format=j1にGETリクエストを送信しますrequests.get()オブジェクトをresponseという名前の変数に保存しますresponse.json()をdataという名前の変数に保存しますdata["current_condition"][0]内にネストされている現在の状況にアクセスし、それらをcurrentという名前の変数に保存しますcurrent["temp_C"]をtemperatureという名前の変数に保存しますcurrent["windspeedKmph"]をwindという名前の変数に保存しますcityという名前の変数を作成し、"London"を代入します- 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のすべてのレッスン
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results