OpenWeatherMap Python?

1 Antwort

data = requests.get(url).json()
temp = ['main']['temp]-273.15
       ^^^^^^^Das hier
Code gibt es so im Internet. Allerdings kommt bei mir folgender Error:

Traceback (most recent call last):
 File "/xyz/wetter.py", line 9, in <module>
  temp = data['main']['temp']-273.15
     ^^^^^^^^^^ Und das hier
KeyError: 'main'

widersprechen sich. Füge Code mit Codeformatierung as is ein.

Du kannst vor der Zuweisung die fehlschlägt einfach mal ein

data = requests.get(url).json()
print(data) 

einfügen, um zu sehen, was da wirklich zurückkommt.

Ich sehe übrigens auch generell keien Fehlerbehandlung, das solltest DU schon machen udn ich möchte heir noch auf folgendes hinweisen:

It should be noted that the success of the call to r.json() does not indicate the success of the response. Some servers may return a JSON object in a failed response (e.g. error details with HTTP 500). Such JSON will be decoded and returned. To check that a request is successful, use r.raise_for_status() or check r.status_code is what you expect.