python提取每日天气
时间: 2023-09-24 22:02:19 浏览: 45
要提取每日天气,需要使用天气数据的API接口。以下是一个使用OpenWeatherMap API的Python示例,可以提取指定城市每日的天气预报:
```python
import requests
# 在OpenWeatherMap上注册账户,获取API key
api_key = "YOUR_API_KEY"
# 指定城市
city = "Shenzhen"
# API URL
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
# 发送请求并获取响应
response = requests.get(url)
# 将响应转换为JSON格式
data = response.json()
# 提取需要的天气信息
weather = data['weather'][0]['description']
temp = data['main']['temp']
humidity = data['main']['humidity']
# 输出结果
print(f"The weather in {city} today is {weather}. Temperature is {temp} Kelvin. Humidity is {humidity}%.")
```
在这个示例中,我们使用OpenWeatherMap API来获取深圳的天气预报。我们从响应中提取了天气描述、温度和湿度,并将它们打印到控制台上。你可以通过更改`city`变量来提取其他城市的天气信息。请注意,这个示例中的温度单位是开尔文(Kelvin),你可以根据需要进行转换。