Код:
Код: Выделить всё
import requests
import json
def get_weather(city, api_key):
base_url = "http://api.openweathermap.org/data/2.5/weather?"
complete_url = base_url + "q=" + city + "&appid=" + api_key + "&units=metric"
response = requests.get(complete_url)
data = response.json()
if data["cod"] != "404":
main = data["main"]
wind = data["wind"]
weather = data["weather"][0]
temperature = main["temp"]
humidity = main["humidity"]
weather_description = weather["description"]
wind_speed = wind["speed"]
print(f"City: {city}")
print(f"Temperature: {temperature}°C")
print(f"Humidity: {humidity}%")
print(f"Weather: {weather_description.capitalize()}")
print(f"Wind Speed: {wind_speed} m/s")
else:
print(f"City {city} not found. Please enter a valid city name.")
if __name__ == "__main__":
api_key = "5a249ac961fe0222f884ded14818c13e"
city = input("Enter city name: ")
get_weather(city, api_key)
Подробнее здесь: https://stackoverflow.com/questions/790 ... -simple-we