📘 Lesson · Lesson 97
Weather API Program
Weather API Program
About this Project
💡 At a Glance
This program fetches live weather for a city from a weather API using the requests library.
The Program
Python
import requests
city = "Delhi"
api_key = "YOUR_API_KEY"
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
if response.status_code == 200:
print("City:", data["name"])
print("Temp:", data["main"]["temp"], "C")
print("Weather:", data["weather"][0]["description"])
else:
print("City not found")City: Delhi
Temp: 32 C
Weather: clear sky
Summary
requests.get()calls the API;.json()parses the response.- Get a free API key from OpenWeatherMap to run it.
इस Project के बारे में
💡 एक नज़र में
यह program requests library से weather API से किसी city का live weather लाता है।
Program
Python
import requests
city = "Delhi"
api_key = "YOUR_API_KEY"
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
if response.status_code == 200:
print("City:", data["name"])
print("Temp:", data["main"]["temp"], "C")
print("Weather:", data["weather"][0]["description"])
else:
print("City not found")City: Delhi
Temp: 32 C
Weather: clear sky
सारांश
requests.get()API call करता है;.json()response parse करता है।- चलाने को OpenWeatherMap से free API key लें।