· 6 years ago · Mar 24, 2020, 07:34 PM
1#кирилл, тебе надо зарегаться на сайте https://openweathermap.org и получить свой api. я думаю ты видишь куда его надо вставить.
2#api можно узнать тут - https://openweathermap.org/api
3def weather():
4 # Python program to find current
5 # using openweathermap api
6
7 # import required modules
8 import requests, json
9
10 # Enter your API key here
11 api_key = "Your_API_Key"
12
13 # base_url variable to store url
14 base_url = "http://api.openweathermap.org/data/2.5/weather?"
15
16 # Give city name
17 send_message("в каком городе вы живете?")
18 city_name = event.obj.text.lower()
19
20 # complete_url variable to store
21 # complete url address
22 complete_url = base_url + "appid=" + api_key + "&q=" + city_name
23
24 # get method of requests module
25 # return response object
26 response = requests.get(complete_url)
27
28 # json method of response object
29 # convert json format data into
30 # python format data
31 x = response.json()
32
33 # Now x contains list of nested dictionaries
34 # Check the value of "cod" key is equal to
35 # "404", means city is found otherwise,
36 # city is not found
37 if x["cod"] != "404":
38
39 # store the value of "main"
40 # key in variable y
41 y = x["main"]
42
43 # store the value corresponding
44 # to the "temp" key of y
45 current_temperature = y["temp"]
46
47 # store the value corresponding
48 # to the "pressure" key of y
49 current_pressure = y["pressure"]
50
51 # store the value corresponding
52 # to the "humidity" key of y
53 current_humidiy = y["humidity"]
54
55 # store the value of "weather"
56 # key in variable z
57 z = x["weather"]
58
59 # store the value corresponding
60 # to the "description" key at
61 # the 0th index of z
62 weather_description = z[0]["description"]
63
64 # print following values
65 print(" Temperature (in kelvin unit) = " +
66 str(current_temperature) +
67 "\n atmospheric pressure (in hPa unit) = " +
68 str(current_pressure) +
69 "\n humidity (in percentage) = " +
70 str(current_humidiy) +
71 "\n description = " +
72 str(weather_description))
73
74 else:
75 print(" City Not Found ")