# weather-collector **Repository Path**: scriptshop/weather-collector ## Basic Information - **Project Name**: weather-collector - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-09-18 - **Last Updated**: 2026-09-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 天气采集器(weather-collector) 通过**中国天气网公开接口**采集城市实时天气,支持定时循环抓取,增量追加 CSV——一个能演示"定时任务"能力的项目。 ## 功能特性 - 内置 12 个常用城市代码,`--cities` 自由组合 - 支持 `--loop` 定时模式:按指定间隔持续抓取(演示定时任务),Ctrl+C 停止 - 增量追加 CSV(带抓取时间戳),适合长期积累数据 - 失败自动重试(指数退避),单个城市失败不影响其它城市 - 兼容接口编码差异(utf-8 / gbk),带 Referer 头 ## 技术栈 Python 3.10+ · requests ## 安装 ```bash pip install -r requirements.txt ``` ## 使用方法 ```bash # 抓一次长沙、北京 python main.py --cities 长沙,北京 # 定时模式:每 10 分钟抓一次(演示定时任务) python main.py --cities 长沙,北京 --loop 600 ``` ### 参数说明 | 参数 | 说明 | 默认值 | |---|---|---| | `--cities` | 城市列表,逗号分隔(可用:北京、上海、广州、深圳、长沙、成都、杭州、武汉、西安、重庆、南京、天津) | `长沙,北京` | | `--out` | 输出 CSV 路径(增量追加) | `output/weather_log.csv` | | `--loop` | 循环间隔秒数;0 = 抓一次退出 | `0` | ### 部署成真正的定时任务 - **Windows**:任务计划程序 → 新建任务 → 触发器(每天/每小时)→ 操作填 `python main.py --cities 长沙` - **Linux / macOS**:`crontab -e` 加一行,如每 30 分钟一次: ``` */30 * * * * cd /path/to/weather-collector && python main.py --cities 长沙,北京 ``` ## 输出示例(真实运行结果) ``` python main.py --cities 长沙,北京 ``` | 抓取时间 | 城市 | 天气 | 温度(℃) | 风向风力 | 湿度 | 发布时间 | |---|---|---|---|---|---|---| | 2026-09-17 16:53:44 | 长沙 | 多云 | 24.5 | 西北风3级 | 71% | 16:30 | | 2026-09-17 16:53:45 | 北京 | 晴 | 30.5 | 南风2级 | 35% | 16:30 | ## 运行截图 【放 1~2 张:终端运行截图 + 累计的天气日志 CSV 截图】 ## 项目结构 ``` weather-collector/ ├── main.py 命令行入口(定时循环) ├── weather.py 抓取逻辑 + 城市代码表 ├── requirements.txt └── output/ (运行后生成) └── weather_log.csv ``` ## 合规声明 本工具仅采集中国天气网公开天气数据,请求间隔 ≥1 秒,遵守访问频率限制。仅供学习与研究使用。 ## License MIT