Menu
Coddy logo textTech

API Connection to Server

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

challenge icon

チャレンジ

中級

OpenWeatherMapは通常APIキーが必要ですが、学習目的のために、登録不要の無料の代替サービスであるOpen-Meteo APIを使用します。

次のURLにGETリクエストを送信してください: https://api.open-meteo.com/v1/forecast

paramsを使用して、以下のパラメータを追加してください:

{
    "latitude": 52.52,
    "longitude": 13.41,
    "current_weather": "true"
}

requests.get()オブジェクトをresponseという名前の変数に保存してください。

response.json()を使用してレスポンスを解析し、dataという名前の変数に保存してください。

data["current_weather"]["temperature"]にアクセスして、現在の気温を表示してください。

自分で試してみよう

import requests

# Step 1: Send GET request with parameters
response = requests.get(
    # Your URL here
    params={
        # Your parameters here
    }
)

# Step 2: Parse JSON response
data = # Your code here

# Step 3: Print current temperature
print("Current temperature:", data["current_weather"]["temperature"], "°C")

Pythonで学ぶAPIのすべてのレッスン