· 6 years ago · Nov 26, 2019, 07:20 PM
1from urllib.request import urlopen
2import json
3import pprint
4
5#
6api_key = "f57f78b88df782edef2d267985a11166" # insert your own API key!!!
7city = "Barcelona"
8country = "es"
9response = urlopen(
10 'http://api.openweathermap.org/data/2.5/weather?q={},{}&units=metric&appid='.format(city, country) + api_key).read()
11data = json.loads(response)
12# pprint.pprint(data)
13# printing info
14# print(data["weather"][0]["description"])
15##################################################
16
17show_weather_template = """
18 Country: {}
19 City: {}
20 Temperature: {}
21 Sunrise: {}
22 Sunset: {}
23 """
24d = data
25print(show_weather_template.format(d["sys"]["country"],
26 d["name"],
27 d["main"]["temp"],
28 d["sys"]["sunrise"],
29 d["sys"]["sunset"], ))