爬取天气预报数据通常需要以下步骤:
准备工作
安装必要的Python库,如`requests`、`BeautifulSoup`、`pandas`和`matplotlib`。
发送HTTP请求
使用`requests`库发送HTTP请求获取网页数据。
解析HTML内容
使用`BeautifulSoup`解析HTML内容,提取有用的信息。
提取天气信息
根据网页结构提取日期、天气状况、温度等信息。
数据存储
将提取的数据保存到文件或数据库中。
数据可视化(可选):
使用`matplotlib`和`pandas`等库创建图表展示数据。
下面是一个简单的示例代码,用于爬取中国天气网的天气预报信息:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
def get_weather_data(city_code):
url = f"http://www.weather.com.cn/weather/{city_code}.shtml"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
提取天气信息
date_tag = soup.find('ul', class_='t clearfix')
date_str = date_tag.h1.string
today_tag = soup.find('p', class_='tem')
try:
temp_high = today_tag.span.string
except AttributeError:
temp_high = today_tag.find_next('p', class_='tem').span.string
temp_low = today_tag.i.string
weather_tag = soup.find('p', class_='wea')
weather_str = weather_tag.string
wind_tag = soup.find('p', class_='win')
wind_str = wind_tag.i.string
return {
'date': date_str,
'high_temp': temp_high,
'low_temp': temp_low,
'weather': weather_str,
'wind': wind_str
}
示例:获取北京市的天气预报
weather_info = get_weather_data('')
print(weather_info)
请注意,网页结构可能会随时间变化,因此代码可能需要根据实际的网页结构进行调整。此外,爬虫可能会受到网站的反爬虫机制限制,可能需要设置`User-Agent`模拟浏览器访问,或者使用代理IP。