-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython__weather_details_using_API.py
More file actions
43 lines (35 loc) · 1.18 KB
/
Python__weather_details_using_API.py
File metadata and controls
43 lines (35 loc) · 1.18 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
40
41
42
43
import requests
api_key = "YOUR_API_KEY_HERE"
weather_url = "https://weatherapi-com.p.rapidapi.com/current.json"
city_name = input("Enter city name: ")
headers = {
'x-rapidapi-key': api_key,
'x-rapidapi-host': "weatherapi-com.p.rapidapi.com"
}
params = {
"q": city_name,
}
weather_response = requests.get(weather_url, headers=headers, params=params)
weather_data = weather_response.json()
if "error" not in weather_data:
location = weather_data["location"]
current = weather_data["current"]
temp_c = current["temp_c"]
feelslike_c = current["feelslike_c"]
humidity = current["humidity"]
pressure_mb = current["pressure_mb"]
condition = current["condition"]
text = condition["text"]
localtime = location["localtime"]
currentLocation = location["name"]
country = location["country"]
print(f"\nLocation: {currentLocation}")
print(f"Country: {country}\n")
print(f"Current Time: {localtime}")
print(f"Temperature: {temp_c}\u00B0C")
print(f"Feels Like: {feelslike_c}\u00B0C")
print(f"Humidity: {humidity}%")
print(f"Pressure: {pressure_mb} hPa")
print(f"Weather Description: {text}")
else:
print("City not found")