GET Request to Server
Lektion 9 von 10 im Kurs API in Python von Coddy.
Aufgabe
MittelNachdem du nun eine ordnungsgemäße Verbindung zu einer API hergestellt hast, senden wir eine GET-Anfrage für einen bestimmten Standort.
- Sende eine GET-Anfrage an:
https://wttr.in/London?format=j1(der Teil?format=j1weist den Dienst an, JSON zurückzugeben) - Speichere das
requests.get()-Objekt in einer Variable namensresponse - Parse die Antwort mit
response.json()und speichere sie in einer Variable namensdata - 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
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results