· 4 years ago · Apr 10, 2021, 04:10 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()
21# Now resp_json contains list of nested dictionaries, which you can print
22print(resp_json)
23# Check the value of "cod" key
24if resp_json["cod"] == 401:
25 print(resp_json["message"])
26 exit(1)
27elif resp_json["cod"] == "404":
28 print(resp_json["message"])
29 exit(2)
30else:
31 #Your code goeas here
32 print("Aktualna pogoda to:")
33 print(resp_json["message"])