· 6 years ago · Feb 14, 2020, 12:54 PM
1#####
2#Read https://openweathermap.org/current
3#####
4
5
6# Python program to find current
7# weather details of any city
8# using openweathermap api
9
10# import required modules
11import requests, json
12
13# Enter your API key here
14api_key = "3afd6135dc7211782ca1448230003fc4"
15
16# base_url variable to store url
17base_url = "http://api.openweathermap.org/data/2.5/weather?"
18
19
20
21# Give city name
22'''city_name = input("Enter city name : ") '''
23
24# complete_url variable to store
25# complete url address
26'''complete_url = base_url + "appid=" + api_key + "&q=" + city_name '''
27
28
29###using latitude and longitude.
30lat = "19.04"
31lon = "47.5"
32coordinate = base_url+"lat="+lat+"&lon="+lon+"&appid="+api_key
33
34
35# get method of requests module
36# return response object
37# yaha pe url pass hoga..
38response = requests.get(coordinate)
39
40# json method of response object
41# convert json format data into
42# python format data
43x = response.json()
44print(x)
45# Now x contains list of nested dictionaries
46# Check the value of "cod" key is equal to
47# "404", means city is found otherwise,
48# city is not found
49if x["cod"] != "404":
50
51 # store the value of "main"
52 # key in variable y
53 y = x["main"]
54
55 # store the value corresponding
56 # to the "temp" key of y
57 current_temperature = y["temp"]
58
59 # store the value corresponding
60 # to the "pressure" key of y
61 current_pressure = y["pressure"]
62
63 # store the value corresponding
64 # to the "humidity" key of y
65 current_humidiy = y["humidity"]
66
67 # store the value of "weather"
68 # key in variable z
69 z = x["weather"]
70
71 # store the value corresponding
72 # to the "description" key at
73 # the 0th index of z
74 weather_description = z[0]["description"]
75
76 # print following values
77 print(" Temperature (in kelvin unit) = " +
78 str(current_temperature) +
79 "\n atmospheric pressure (in hPa unit) = " +
80 str(current_pressure) +
81 "\n humidity (in percentage) = " +
82 str(current_humidiy) +
83 "\n description = " +
84 str(weather_description))
85
86else:
87 print(" City Not Found ")