Menu
Coddy logo textTech

GET Request to Server

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

challenge icon

チャレンジ

中級

APIとの適切な接続が確立できたので、特定の場所に対してGETリクエストを送信してみましょう。

  1. https://wttr.in/London?format=j1 にGETリクエストを送信します(?format=j1 の部分は、サービスにJSONを返すように指示するものです)
  2. requests.get() オブジェクトを response という名前の変数に保存します
  3. response.json() を使用してレスポンスを解析し、data という名前の変数に保存します
  4. 現在の状況は 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のすべてのレッスン