Menu
Coddy logo textTech

GET Request to Server

Lektion 9 von 10 im Kurs API in Python von Coddy.

challenge icon

Aufgabe

Mittel

Nachdem du nun eine ordnungsgemäße Verbindung zu einer API hergestellt hast, senden wir eine GET-Anfrage für einen bestimmten Standort.

  1. Sende eine GET-Anfrage an: https://wttr.in/London?format=j1 (der Teil ?format=j1 weist den Dienst an, JSON zurückzugeben)
  2. Speichere das requests.get()-Objekt in einer Variable namens response
  3. Parse die Antwort mit response.json() und speichere sie in einer Variable namens data
  4. Die aktuellen Bedingungen befinden sich in data["current_condition"][0]. Extrahiere die Temperatur (temp_C) und die Beschreibung (weatherDesc[0]["value"]) und gib beides aus

Probier es selbst

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"])

Alle Lektionen in API in Python