GET Request to Server
Coddyの「Pythonで学ぶAPI」コースのレッスン 9/10。
チャレンジ
中級APIとの適切な接続が確立できたので、特定の場所に対してGETリクエストを送信してみましょう。
https://wttr.in/London?format=j1にGETリクエストを送信します(?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のすべてのレッスン
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results