· 4 years ago · Apr 10, 2021, 04:38 PM
1import requests, json
2
3# Enter your API key here
4api_key = "408115c114cc80d5bbfd559bc65f675e"
5
6# base_url variable to store url
7base_url = "http://api.openweathermap.org/data/2.5/weather?"
8
9# Give city name
10city_name = "Wroclaw"
11
12# complete url address
13complete_url = base_url + "appid=" + api_key + "&q=" + city_name + "&units=metric"
14
15# get method of requests module
16# return response object
17response = requests.get(complete_url)
18
19# convert json format data into python format data
20resp_json = response.json()
21pogoda = resp_json["weather"]
22temperatura = resp_json["main"]
23opis = pogoda[0]
24# Now resp_json contains list of nested dictionaries, which you can print
25# Check the value of "cod" key
26if resp_json["cod"] == 401:
27 print(resp_json["message"])
28 exit(1)
29elif resp_json["cod"] == "404":
30 print(resp_json["message"])
31 exit(2)
32else:
33 #Your code goeas here
34 print("Aktualna pogoda to: " + opis["main"] + " (" + opis["description"] + ")")
35 print("Aktualna temperatura to: " + str(temperatura["temp"]) + "*C")