how to use open weather weather api for pytho

Solutions on MaxInterview for how to use open weather weather api for pytho by the best coders in the world

showing results for - "how to use open weather weather api for pytho"
Kenny
26 Jul 2018
1# importing requests and json
2import requests, json
3# base URL
4BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
5# City Name CITY = "Hyderabad"
6# API key API_KEY = "Your API Key"
7# upadting the URL
8URL = BASE_URL + "q=" + CITY + "&appid=" + API_KEY
9# HTTP request
10response = requests.get(URL)
11# checking the status code of the request
12if response.status_code == 200:
13   # getting data in the json format
14   data = response.json()
15   # getting the main dict block
16   main = data['main']
17   # getting temperature
18   temperature = main['temp']
19   # getting the humidity
20   humidity = main['humidity']
21   # getting the pressure
22   pressure = main['pressure']
23   # weather report
24   report = data['weather']
25   print(f"{CITY:-^30}")
26   print(f"Temperature: {temperature}")
27   print(f"Humidity: {humidity}")
28   print(f"Pressure: {pressure}")
29   print(f"Weather Report: {report[0]['description']}")
30else:
31   # showing the error message
32   print("Error in the HTTP request")