怪物卡车机器人
83.87MB · 2025-09-30
在数据驱动的时代,天气预报数据是气象分析、农业规划、物流调度等场景的重要基础。本文将通过一个完整案例,教你如何用Python的Requests库获取网页内容,再用BeautifulSoup解析HTML,最终实现天气预报数据的自动化采集。整个过程不涉及复杂理论,只讲实战技巧,即使你是编程新手也能轻松上手。
在Python爬虫领域,Scrapy框架功能强大但学习曲线陡峭,Selenium能处理动态页面但效率较低。对于天气预报这类结构化数据采集,Requests+BeautifulSoup的组合具有显著优势:
以中国天气网为例,其省级天气页面采用标准HTML结构,非常适合用这种组合采集。我们以采集"北京市天气预报"为例,展示完整实现过程。
pip install requests beautifulsoup4 fake-useragent
requests
:发送HTTP请求beautifulsoup4
:解析HTMLfake-useragent
:生成随机User-Agent(反爬必备)import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
def get_weather_page(url):
headers = {'User-Agent': UserAgent().random}
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # 检查请求是否成功
response.encoding = 'utf-8' # 设置编码
return response.text
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
return None
# 测试请求
url = "http://www.weather.com.cn/textFC/hb.shtml" # 华北地区天气
html_content = get_weather_page(url)
if html_content:
print("获取页面成功")
关键点说明:
打开中国天气网华北地区页面,右键检查元素,找到天气数据所在的HTML结构:
<div class="conMidtab">
<div class="conMidtab2">
<table class="twoInner">
<tr>
<td class="time">08时</td>
<td>晴</td>
<td>-2℃</td>
<td>3级</td>
</tr>
<!-- 更多行... -->
</table>
</div>
</div>
关键特征:
class="twoInner"
的表格中
def parse_weather_data(html):
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table', class_='twoInner')
if not table:
print("未找到天气表格")
return []
weather_data = []
rows = table.find_all('tr')[1:] # 跳过表头
for row in rows:
cols = row.find_all('td')
if len(cols) >= 4:
data = {
'time': cols[0].get_text(strip=True),
'weather': cols[1].get_text(strip=True),
'temperature': cols[2].get_text(strip=True),
'wind': cols[3].get_text(strip=True)
}
weather_data.append(data)
return weather_data
# 测试解析
if html_content:
data = parse_weather_data(html_content)
for item in data[:3]: # 打印前3条
print(item)
输出示例:
{'time': '08时', 'weather': '晴', 'temperature': '-2℃', 'wind': '3级'}
{'time': '11时', 'weather': '晴', 'temperature': '3℃', 'wind': '2级'}
{'time': '14时', 'weather': '多云', 'temperature': '5℃', 'wind': '2级'}
table
是否存在,避免程序崩溃get_text(strip=True)
去除空白字符将上述代码整合为完整脚本,并添加数据存储功能:
import csv
from datetime import datetime
def save_to_csv(data, filename='weather.csv'):
if not data:
print("无数据可保存")
return
# 添加采集时间
for item in data:
item['update_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# 写入CSV文件
with open(filename, 'w', newline='', encoding='utf-8-sig') as f:
writer = csv.DictWriter(f, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
print(f"数据已保存到 {filename}")
def main():
url = "http://www.weather.com.cn/textFC/hb.shtml"
html = get_weather_page(url)
if html:
data = parse_weather_data(html)
save_to_csv(data)
if __name__ == '__main__':
main()
执行结果:
程序会在当前目录生成weather.csv文件,包含采集时间和所有天气数据。
通过修改URL参数实现:
CITIES = {
'北京': 'hb.shtml', # 华北
'上海': 'hd.shtml', # 华东
# 添加更多城市...
}
def batch_collect():
for city, path in CITIES.items():
url = f"http://www.weather.com.cn/textFC/{path}"
html = get_weather_page(url)
if html:
data = parse_weather_data(html)
for item in data:
item['city'] = city # 添加城市字段
save_to_csv(data, f'weather_{city.lower()}.csv')
使用schedule
库实现每天8点自动采集:
import schedule
import time
def job():
print("开始定时采集...")
main()
schedule.every().day.at("08:00").do(job)
while True:
schedule.run_pending()
time.sleep(60)
用Matplotlib简单绘制温度变化曲线:
import matplotlib.pyplot as plt
def plot_temperature(data):
times = [item['time'] for item in data]
temps = [int(item['temperature'].replace('℃', '')) for item in data]
plt.figure(figsize=(10, 4))
plt.plot(times, temps, marker='o')
plt.title('北京今日温度变化')
plt.xlabel('时间')
plt.ylabel('温度(℃)')
plt.grid(True)
plt.savefig('temperature.png')
plt.show()
Q1:被网站封IP怎么办?
A:立即启用备用代理池,建议使用住宅代理(如站大爷IP代理),配合每请求更换IP策略。更简单的方案是设置请求间隔:
import time
import random
def safe_request(url):
time.sleep(random.uniform(1, 3)) # 随机延迟1-3秒
return get_weather_page(url)
Q2:网站结构变了怎么办?
A:定期检查解析代码是否还能正常工作。建议:
class_='twoInner'
)提取为配置变量try-except
捕获解析异常Q3:如何采集动态加载的数据?
A:对于JavaScript渲染的数据,有两种方案:
Q4:采集频率应该设置多少?
A:遵循robots.txt协议和网站服务条款。一般建议:
Q5:如何存储大量历史数据?
A:根据数据量选择:
通过本文,你已掌握:
实际应用中,你可能需要处理:
建议从简单需求开始,逐步添加复杂功能。记住:爬虫开发的核心是模拟人类浏览行为,保持请求的随机性和合理性是长期运行的关键。