python爬虫爬取一年天气数据
时间: 2024-01-06 13:06:08 浏览: 192
以下是使用Python爬虫爬取一年天气数据的示例代码:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
def get_weather_data(year):
# 创建一个空的DataFrame用于保存数据
weather_data = pd.DataFrame(columns=['日期', '天气', '最高温度', '最低温度'])
# 循环遍历每个月份
for month in range(1, 13):
# 构造URL
url = f'http://www.weather.com.cn/weather/{year}{month:02d}.shtml'
# 发送HTTP请求获取页面内容
response = requests.get(url)
response.encoding = 'utf-8'
html = response.text
# 使用BeautifulSoup解析页面内容
soup = BeautifulSoup(html, 'html.parser')
# 获取每天的天气数据
days = soup.select('.t li')
# 循环遍历每天的数据
for day in days:
date = day.select('.time')[0].text.strip()
weather = day.select('.wea')[0].text.strip()
high_temp = day.select('.tem span')[0].text.strip()
low_temp = day.select('.tem i')[0].text.strip()
# 将数据添加到DataFrame中
weather_data = weather_data.append({'日期': date, '天气': weather, '最高温度': high_temp, '最低温度': low_temp}, ignore_index=True)
return weather_data
# 调用函数获取2021年的天气数据
weather_data_2021 = get_weather_data(2021)
# 打印结果
print(weather_data_2021)
```
阅读全文