282 lines
7.1 KiB
Markdown
282 lines
7.1 KiB
Markdown
# 广西政府采购网公告监控系统
|
||
|
||
广西政府采购网公告爬取和监控的智能系统,支持多种公告类型的自动爬取、智能筛选、数据存储和通知推送。
|
||
|
||
## 功能特性
|
||
|
||
### 🕷️ 智能爬虫监控
|
||
- 自动爬取广西政府采购网(zfcg.gxzf.gov.cn)的各类采购公告
|
||
- 支持10+种公告类型:采购公告、结果公告、合同公告、更正公告、招标文件预公示、单一来源公示、电子卖场公示、履约验收公示、工程类公告、框架协议征集公告等
|
||
- 智能反爬虫机制:随机User-Agent、代理轮换、请求延迟控制
|
||
|
||
### 🎯 多维度智能筛选
|
||
- **关键词过滤**:支持多关键词精确匹配和模糊匹配
|
||
- **日期范围筛选**:支持开始日期和结束日期过滤
|
||
- **自动去重**:基于内容哈希的智能去重机制
|
||
- **来源筛选**:按公告类型进行筛选
|
||
|
||
### 💾 数据持久化存储
|
||
- **PostgreSQL数据库**:高效的数据存储和查询
|
||
- **自动清理**:默认90天数据保留,可配置
|
||
- **性能优化**:连接池、索引优化、批量操作
|
||
|
||
### 🔔 高可用性保障
|
||
- **重试机制**:指数退避重试策略
|
||
- **超时控制**:连接超时和读取超时设置
|
||
- **幂等操作**:防止重复处理
|
||
- **异常处理**:完善的异常捕获和恢复
|
||
- **结构化日志**:详细的运行日志记录
|
||
|
||
### 📱 通知系统
|
||
- **企业微信通知**:支持文本和Markdown格式消息
|
||
- **实时推送**:新公告发现即时通知
|
||
- **系统告警**:异常情况自动告警
|
||
|
||
### 📊 Markdown输出
|
||
- **自动生成**:将公告输出为Markdown格式文件(onu.md)
|
||
- **结构化展示**:按来源分组、时间排序
|
||
- **今日高亮**:突出显示今日发布的公告
|
||
|
||
## 安装和使用
|
||
|
||
### 环境要求
|
||
- Python 3.8+
|
||
- PostgreSQL 12+
|
||
|
||
### 安装步骤
|
||
|
||
1. **克隆项目**
|
||
```bash
|
||
cd /path/to/your/workspace
|
||
# 项目已创建在 gx_gp_monitor 目录下
|
||
```
|
||
|
||
2. **创建虚拟环境**
|
||
```bash
|
||
python3 -m venv venv
|
||
source venv/bin/activate # Linux/Mac
|
||
# 或
|
||
venv\Scripts\activate # Windows
|
||
```
|
||
|
||
3. **安装依赖**
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
4. **配置数据库**
|
||
```bash
|
||
# 创建PostgreSQL数据库
|
||
createdb gx-gp-notify
|
||
|
||
# 修改配置文件 config/config.yaml 中的数据库连接信息
|
||
```
|
||
|
||
5. **修改配置**
|
||
```bash
|
||
# 编辑 config/config.yaml 文件
|
||
# 设置数据库连接、企业微信配置、关键词等
|
||
```
|
||
|
||
### 使用方法
|
||
|
||
#### 命令行使用
|
||
|
||
```bash
|
||
# 查看帮助
|
||
python main.py --help
|
||
|
||
# 执行一次爬取
|
||
python main.py crawl
|
||
|
||
# 带参数爬取
|
||
python main.py crawl --keywords "大化" "信息化" --max-pages 5
|
||
|
||
# 启动调度器(定时任务)
|
||
python main.py scheduler
|
||
|
||
# 数据清理
|
||
python main.py cleanup --days 30
|
||
|
||
# 查看系统状态
|
||
python main.py status
|
||
```
|
||
|
||
#### 编程接口使用
|
||
|
||
```python
|
||
from gx_gp_monitor.crawler.spider import crawl_announcements
|
||
from gx_gp_monitor.filters.filters import filter_from_config
|
||
from gx_gp_monitor.storage.postgresql import save_announcements_to_storage
|
||
from gx_gp_monitor.storage.md_generator import generate_onu_md
|
||
from gx_gp_monitor.notification.wechat import send_announcements_notification
|
||
|
||
# 执行爬取
|
||
results = crawl_announcements()
|
||
|
||
# 筛选公告
|
||
filter_obj = filter_from_config()
|
||
filtered_announcements, stats = filter_obj.filter(all_announcements)
|
||
|
||
# 保存到数据库
|
||
saved_count = save_announcements_to_storage(filtered_announcements)
|
||
|
||
# 生成Markdown文件
|
||
generate_onu_md(filtered_announcements)
|
||
|
||
# 发送通知
|
||
send_announcements_notification(filtered_announcements)
|
||
```
|
||
|
||
## 配置说明
|
||
|
||
### 主要配置文件:`config/config.yaml`
|
||
|
||
```yaml
|
||
# 调试模式
|
||
debug: false
|
||
|
||
# 日志配置
|
||
log_level: INFO
|
||
log_file: logs/gx_gp_monitor.log
|
||
|
||
# 爬虫配置
|
||
crawler:
|
||
base_url: "https://zfcg.gxzf.gov.cn"
|
||
timeout: 30
|
||
max_retries: 3
|
||
keyword: ["大化", "信息化"] # 关键词筛选
|
||
max_pages: 10
|
||
|
||
# 数据库配置
|
||
database:
|
||
enabled: true
|
||
host: "localhost"
|
||
port: 5432
|
||
name: "gx-gp-notify"
|
||
user: "your_user"
|
||
password: "your_password"
|
||
data_retention_days: 90
|
||
|
||
# 企业微信配置
|
||
wechat_app:
|
||
enabled: true
|
||
corp_id: "your_corp_id"
|
||
agent_id: "your_agent_id"
|
||
secret: "your_secret"
|
||
token: "your_token"
|
||
encoding_aes_key: "your_aes_key"
|
||
|
||
# 调度配置
|
||
scheduler:
|
||
enabled: true
|
||
jobs:
|
||
- name: "daily_crawl"
|
||
cron: "0 8,14,18 * * *"
|
||
enabled: true
|
||
```
|
||
|
||
### 公告来源配置
|
||
|
||
系统支持以下公告类型:
|
||
|
||
- `ZcyAnnouncement1`: 采购公告
|
||
- `ZcyAnnouncement2`: 结果公告
|
||
- `ZcyAnnouncement3`: 合同公告
|
||
- `ZcyAnnouncement4`: 更正公告
|
||
- `ZcyAnnouncement5`: 招标文件预公示
|
||
- `ZcyAnnouncement6`: 单一来源公示
|
||
- `ZcyAnnouncement7`: 电子卖场公示
|
||
- `ZcyAnnouncement10`: 履约验收公示
|
||
- `ZcyAnnouncement11`: 工程类公告
|
||
- `ZcyAnnouncement20`: 框架协议征集公告
|
||
- `ZcyAnnouncement21`: 框架协议入围结果公告
|
||
- `ZcyAnnouncement23`: 框架协议成交结果汇总公告
|
||
- `61-266648`: 采购意向公开
|
||
|
||
## 架构设计
|
||
|
||
```
|
||
gx_gp_monitor/
|
||
├── config/ # 配置管理
|
||
├── core/ # 核心模块
|
||
│ ├── models.py # 数据模型
|
||
│ ├── config_manager.py # 配置管理
|
||
│ ├── database.py # 数据库操作
|
||
│ ├── logger.py # 日志管理
|
||
│ └── reliability.py # 高可用性
|
||
├── crawler/ # 爬虫模块
|
||
│ ├── spider.py # 爬虫核心
|
||
│ └── parsers.py # 数据解析
|
||
├── filters/ # 筛选模块
|
||
├── storage/ # 存储模块
|
||
│ ├── postgresql.py # PostgreSQL存储
|
||
│ └── md_generator.py # Markdown生成
|
||
├── notification/ # 通知模块
|
||
├── scheduler/ # 调度模块
|
||
└── main.py # 主程序入口
|
||
```
|
||
|
||
## 日志和监控
|
||
|
||
### 日志文件
|
||
- 默认日志文件:`logs/gx_gp_monitor.log`
|
||
- 日志轮转:10MB大小限制,保留5个备份文件
|
||
- 日志级别:DEBUG、INFO、WARNING、ERROR、CRITICAL
|
||
|
||
### 监控指标
|
||
- 爬取统计:成功/失败次数、响应时间
|
||
- 数据统计:公告数量、新增数量、去重统计
|
||
- 系统状态:内存使用、磁盘空间、健康检查
|
||
|
||
## 故障排除
|
||
|
||
### 常见问题
|
||
|
||
1. **数据库连接失败**
|
||
- 检查PostgreSQL服务是否运行
|
||
- 验证数据库连接配置
|
||
- 确认用户权限
|
||
|
||
2. **爬虫请求失败**
|
||
- 检查网络连接
|
||
- 验证目标网站是否可访问
|
||
- 调整请求间隔和重试策略
|
||
|
||
3. **企业微信通知失败**
|
||
- 检查企业微信配置
|
||
- 验证应用ID和密钥
|
||
- 确认网络能访问企业微信API
|
||
|
||
4. **权限问题**
|
||
- 确保日志目录和输出文件目录有写入权限
|
||
- 检查数据库用户权限
|
||
|
||
### 调试模式
|
||
|
||
启用调试模式获取更详细的日志:
|
||
|
||
```yaml
|
||
debug: true
|
||
log_level: DEBUG
|
||
```
|
||
|
||
## 许可证
|
||
|
||
本项目采用 MIT 许可证。
|
||
|
||
## 贡献
|
||
|
||
欢迎提交Issue和Pull Request来改进这个项目。
|
||
|
||
## 版本历史
|
||
|
||
- **v1.0.0** (2024-01-XX)
|
||
- 初始版本发布
|
||
- 支持10+种公告类型爬取
|
||
- 实现智能筛选和去重
|
||
- PostgreSQL数据存储
|
||
- 企业微信通知
|
||
- Markdown文件输出
|
||
- 定时任务调度
|