Menu
Coddy logo textTech

חיבור API לשרת

שיעור 8 מתוך 10 בקורס API ב-Python של Coddy.

challenge icon

אתגר

בינוני

OpenWeatherMap דורש בדרך כלל מפתח API, אבל לצורכי למידה נשתמש בחלופה חינמית שלא דורשת הרשמה: Open-Meteo API.

שלחו בקשת 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")

כל השיעורים ביחידה API ב-Python

תרגלו בעצמכם: קומפיילר Python אונליין