✅ Python Project Simple Weather App with OpenWeather API 🌦️📲
✅ Python Project : Simple Weather App with OpenWeather API 🌦️📲
📌 Task:
Build a Python program that fetches live weather details of any city using the OpenWeatherMap API.
🧠 What You’ll Learn:
-
Sending API requests with query parameters
-
Parsing weather info from JSON data
-
Showing results in a readable format
✅ Python Code Example
import requests
def fetch_weather(city_name):
api_token = "YOUR_API_KEY" # Replace with your OpenWeather API key
weather_url = "https://api.openweathermap.org/data/2.5/weather"
query = {
"q": city_name,
"appid": api_token,
"units": "metric" # Celsius (use 'imperial' for Fahrenheit)
}
response = requests.get(weather_url, params=query)
if response.status_code == 200:
info = response.json()
print(f"📍 City: {city_name}\n")
print(f"🌡 Temperature: {info['main']['temp']}°C")
print(f"☁ Weather: {info['weather'][0]['description'].title()}")
print(f"💧 Humidity: {info['main']['humidity']}%")
print(f"💨 Wind: {info['wind']['speed']} m/s")
else:
print("⚠️ Error: City not found or API issue.")
# Example Run
fetch_weather("Delhi")
📥 How It Works:
-
Sends a request to OpenWeather API with city + API key
-
API returns weather in JSON format
-
Extracts and displays temperature, condition, humidity, wind speed
🔧 Try Yourself:
-
Use
input()to let users type a city name -
Show sunrise/sunset timings or pressure
-
Save weather logs to a file for later use
💡 Use Cases:
✅ Quick command-line weather tool
✅ Weather widget for dashboards
✅ Practice for API authentication & parsing
Comments
Post a Comment