Python脚本下载OpenWeatherMap天气预报数据
以下是一个使用Python编写的脚本,用于从OpenWeatherMap API下载天气预报数据:\n\npython\nimport requests\nimport json\n\ndef download_weather_forecast(api_key, city):\n url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"\n response = requests.get(url)\n data = response.json()\n\n if data["cod"] == "404":\n print("City not found.")\n return\n\n weather = data["weather"][0]["main"]\n temperature = data["main"]["temp"]\n humidity = data["main"]["humidity"]\n wind_speed = data["wind"]["speed"]\n\n print(f"Weather: {weather}")\n print(f"Temperature: {temperature} K")\n print(f"Humidity: {humidity}%")\n print(f"Wind Speed: {wind_speed} m/s")\n\n# 替换为您的OpenWeatherMap API密钥\napi_key = "YOUR_API_KEY"\n\n# 替换为您要查询的城市名称\ncity = "Beijing"\n\ndownload_weather_forecast(api_key, city)\n\n\n请注意,您需要在OpenWeatherMap网站上注册并获取API密钥,然后将其替换为代码中的YOUR_API_KEY。\n\n此脚本将使用OpenWeatherMap API来获取指定城市的天气预报数据,并打印出天气、温度、湿度和风速信息。您可以根据需要自定义脚本以实现更多功能。
原文地址: https://www.cveoy.top/t/topic/p5cJ 著作权归作者所有。请勿转载和采集!