-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweatherapi.py
More file actions
39 lines (30 loc) · 1.25 KB
/
weatherapi.py
File metadata and controls
39 lines (30 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# WEATHER API FETCHER
#code for weather API fetcher
import requests
def fetch_weather(api_key, city):
base_url = 'http://api.openweathermap.org/data/2.5/weather'
params = {
'q': city,
'units': 'metric', # Use 'imperial' for Fahrenheit
'appid': api_key
}
# Make a get request to the OpenWeatherMap API
response = requests.get(base_url, params=params)
if response.status_code == 200:
# Extract data in JSON format
weather_data = response.json()
# Extract relevant information
description = weather_data['weather'][0]['description']
temperature = weather_data['main']['temp']
humidity = weather_data['main']['humidity']
# Print the weather information
print(f"Weather in {city}: {description}")
print(f"Temperature: {temperature}°C")
print(f"Humidity: {humidity}%")
else:
print(f"Error fetching weather data: {response.status_code}")
if __name__ == "__main__":
# Prompt the user to enter API key and city name
api_key = input("Enter your OpenWeatherMap API key: ").strip()
city = input("Enter city name: ").strip()
fetch_weather(api_key, city)