Add Support the other website
This commit is contained in:
197
BUG_FIXES_SUMMARY.md
Normal file
197
BUG_FIXES_SUMMARY.md
Normal file
@@ -0,0 +1,197 @@
|
||||
# 爬虫Bug修复总结
|
||||
|
||||
## 修复的问题列表
|
||||
|
||||
### 1. 新华网 - 不保存文章内容
|
||||
**问题**: 新华网爬取的文章内容没有被正确保存
|
||||
**修复**:
|
||||
- 更新了文章结构识别逻辑,增加了更多内容选择器
|
||||
- 修复了文章页面判断逻辑
|
||||
- 添加了对新华网特定HTML结构的支持
|
||||
|
||||
### 2. 中国政府网 - 两个标题问题
|
||||
**问题**: 爬取到文章后,打开文章详情会有两个标题存在
|
||||
**修复**:
|
||||
- 优化了标题提取逻辑,优先选择带有class="title"的h1标签
|
||||
- 改进了标题去重机制
|
||||
|
||||
### 3. 人民网 - 乱码和404问题
|
||||
**问题**: 爬取文章后会乱码,会有404,视频没有下载下来
|
||||
**修复**:
|
||||
- 添加了特殊的请求头配置
|
||||
- 修复了编码问题,确保使用UTF-8编码
|
||||
- 改进了错误处理机制
|
||||
- 优化了视频下载逻辑
|
||||
|
||||
### 4. 央视网 - 没有保存视频
|
||||
**问题**: 央视网的视频没有被正确下载和保存
|
||||
**修复**:
|
||||
- 增加了对data-src、data-url等视频源属性的支持
|
||||
- 添加了央视网特定的视频处理逻辑
|
||||
- 改进了视频下载的错误处理和日志记录
|
||||
|
||||
### 5. 求是网 - 两个标题问题
|
||||
**问题**: 打开文章详情会有两个标题
|
||||
**修复**:
|
||||
- 优化了标题提取逻辑
|
||||
- 改进了标题去重机制
|
||||
|
||||
### 6. 解放军报 - 类别爬取问题
|
||||
**问题**: 会把类别都爬下来
|
||||
**修复**:
|
||||
- 改进了文章页面判断逻辑
|
||||
- 优化了内容区域识别
|
||||
|
||||
### 7. 光明日报 - 不保存文章内容
|
||||
**问题**: 文章内容没有被正确保存
|
||||
**修复**:
|
||||
- 增加了更多内容选择器
|
||||
- 添加了对article-body等特定class的支持
|
||||
|
||||
### 8. 中国日报 - 不保存文章内容
|
||||
**问题**: 文章内容没有被正确保存
|
||||
**修复**:
|
||||
- 增加了更多内容选择器
|
||||
- 添加了对article-body等特定class的支持
|
||||
|
||||
### 9. 工人日报 - 不保存文章内容
|
||||
**问题**: 文章内容没有被正确保存
|
||||
**修复**:
|
||||
- 增加了更多内容选择器
|
||||
- 添加了对article-body等特定class的支持
|
||||
|
||||
### 10. 科技日报 - 无法爬取
|
||||
**问题**: 无法正常爬取文章
|
||||
**修复**:
|
||||
- 更新了文章结构识别逻辑
|
||||
- 改进了文章页面判断逻辑
|
||||
|
||||
### 11. 人民政协报 - 爬取错误
|
||||
**问题**: 爬取过程中出现错误
|
||||
**修复**:
|
||||
- 优化了错误处理机制
|
||||
- 改进了文章结构识别
|
||||
|
||||
### 12. 中国纪检监察报 - 无法爬取
|
||||
**问题**: 无法正常爬取文章
|
||||
**修复**:
|
||||
- 更新了文章结构识别逻辑
|
||||
- 改进了文章页面判断逻辑
|
||||
|
||||
### 13. 中国新闻社 - 爬取非文章部分
|
||||
**问题**: 爬取了非文章的部分内容
|
||||
**修复**:
|
||||
- 改进了文章页面判断逻辑
|
||||
- 优化了内容区域识别
|
||||
|
||||
### 14. 学习时报 - 不保存文章内容
|
||||
**问题**: 文章内容没有被正确保存
|
||||
**修复**:
|
||||
- 增加了更多内容选择器
|
||||
- 添加了对article-body等特定class的支持
|
||||
|
||||
### 15. 中国青年报 - 无法爬取
|
||||
**问题**: 无法正常爬取文章
|
||||
**修复**:
|
||||
- 更新了文章结构识别逻辑
|
||||
- 改进了文章页面判断逻辑
|
||||
|
||||
### 16. 中国妇女报 - 不保存文章内容
|
||||
**问题**: 文章内容没有被正确保存
|
||||
**修复**:
|
||||
- 增加了更多内容选择器
|
||||
- 添加了对article-body等特定class的支持
|
||||
|
||||
### 17. 法治日报 - 无法爬取
|
||||
**问题**: 无法正常爬取文章
|
||||
**修复**:
|
||||
- 更新了文章结构识别逻辑
|
||||
- 改进了文章页面判断逻辑
|
||||
|
||||
### 18. 农民日报 - 正文未被爬取
|
||||
**问题**: 文章正文没有被正确爬取
|
||||
**修复**:
|
||||
- 增加了更多内容选择器
|
||||
- 添加了对article-body等特定class的支持
|
||||
|
||||
### 19. 学习强国 - 无法爬取
|
||||
**问题**: 无法正常爬取文章
|
||||
**修复**:
|
||||
- 更新了文章结构识别逻辑
|
||||
- 改进了文章页面判断逻辑
|
||||
|
||||
### 20. 旗帜网 - 不保存文章内容
|
||||
**问题**: 文章内容没有被正确保存
|
||||
**修复**:
|
||||
- 增加了更多内容选择器
|
||||
- 添加了对article-body等特定class的支持
|
||||
|
||||
### 21. 中国网 - 不保存文章内容
|
||||
**问题**: 文章内容没有被正确保存
|
||||
**修复**:
|
||||
- 增加了更多内容选择器
|
||||
- 添加了对article-body等特定class的支持
|
||||
|
||||
## 主要修复内容
|
||||
|
||||
### 1. 文章结构识别优化
|
||||
- 为每个网站添加了更精确的标题和内容选择器
|
||||
- 增加了对多种HTML结构的支持
|
||||
- 优化了选择器的优先级
|
||||
|
||||
### 2. 文章页面判断改进
|
||||
- 改进了文章页面的识别逻辑
|
||||
- 增加了URL路径模式的判断
|
||||
- 优化了页面类型识别
|
||||
|
||||
### 3. 编码和请求优化
|
||||
- 修复了人民网的乱码问题
|
||||
- 添加了特殊的请求头配置
|
||||
- 改进了错误处理机制
|
||||
|
||||
### 4. 视频下载增强
|
||||
- 增加了对多种视频源属性的支持
|
||||
- 添加了央视网特定的视频处理
|
||||
- 改进了视频下载的错误处理
|
||||
|
||||
### 5. URL配置更新
|
||||
- 将部分网站的URL从HTTP更新为HTTPS
|
||||
- 确保使用正确的域名和协议
|
||||
|
||||
## 技术改进
|
||||
|
||||
### 1. 错误处理
|
||||
- 添加了更完善的异常处理
|
||||
- 改进了错误日志记录
|
||||
- 增加了重试机制
|
||||
|
||||
### 2. 内容识别
|
||||
- 增加了更多内容选择器
|
||||
- 优化了选择器的优先级
|
||||
- 添加了对特殊HTML结构的支持
|
||||
|
||||
### 3. 媒体处理
|
||||
- 改进了图片和视频的下载逻辑
|
||||
- 增加了对多种媒体源的支持
|
||||
- 优化了媒体文件的保存
|
||||
|
||||
### 4. 性能优化
|
||||
- 改进了请求超时设置
|
||||
- 优化了编码处理
|
||||
- 减少了不必要的请求
|
||||
|
||||
## 测试建议
|
||||
|
||||
1. **单个测试**: 对每个修复的网站进行单独测试
|
||||
2. **批量测试**: 使用批量爬取命令测试所有网站
|
||||
3. **内容验证**: 检查爬取的文章内容是否完整
|
||||
4. **媒体验证**: 确认图片和视频是否正确下载
|
||||
5. **错误监控**: 监控爬取过程中的错误日志
|
||||
|
||||
## 后续优化建议
|
||||
|
||||
1. **动态适配**: 考虑添加动态适配机制,自动适应网站结构变化
|
||||
2. **智能识别**: 使用机器学习技术提高内容识别的准确性
|
||||
3. **反爬虫处理**: 添加更复杂的反爬虫绕过机制
|
||||
4. **性能监控**: 添加性能监控和统计功能
|
||||
5. **内容质量**: 增加内容质量检测和过滤机制
|
||||
178
CRAWLER_README.md
Normal file
178
CRAWLER_README.md
Normal file
@@ -0,0 +1,178 @@
|
||||
# 中央主流媒体爬虫系统
|
||||
|
||||
本项目是一个专门用于爬取中央主流媒体的Django爬虫系统,支持爬取18家中央主流媒体及其子网站、客户端和新媒体平台。
|
||||
|
||||
## 支持的媒体列表
|
||||
|
||||
### 18家中央主流媒体
|
||||
1. **人民日报** - 人民网、人民日报客户端、人民日报报纸
|
||||
2. **新华社** - 新华网、新华网主站、新华社移动端
|
||||
3. **中央广播电视总台** - 央视网、央视新闻、央视移动端
|
||||
4. **求是** - 求是网、求是移动端
|
||||
5. **解放军报** - 解放军报、解放军报移动端
|
||||
6. **光明日报** - 光明日报、光明日报移动端
|
||||
7. **经济日报** - 经济日报、经济日报移动端
|
||||
8. **中国日报** - 中国日报、中国日报移动端
|
||||
9. **工人日报** - 工人日报、工人日报移动端
|
||||
10. **科技日报** - 科技日报、科技日报移动端
|
||||
11. **人民政协报** - 人民政协报、人民政协报移动端
|
||||
12. **中国纪检监察报** - 中国纪检监察报、中国纪检监察报移动端
|
||||
13. **中国新闻社** - 中国新闻社、中国新闻社移动端
|
||||
14. **学习时报** - 学习时报、学习时报移动端
|
||||
15. **中国青年报** - 中国青年报、中国青年报移动端
|
||||
16. **中国妇女报** - 中国妇女报、中国妇女报移动端
|
||||
17. **法治日报** - 法治日报、法治日报移动端
|
||||
18. **农民日报** - 农民日报、农民日报移动端
|
||||
|
||||
### 特殊平台
|
||||
19. **学习强国** - 中央媒体学习号及省级以上学习平台
|
||||
20. **旗帜网** - 旗帜网及其移动端
|
||||
21. **中国网** - 主网及中国网一省份(不转发二级子网站)
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 1. 单个媒体爬取
|
||||
|
||||
```bash
|
||||
# 爬取人民日报所有平台
|
||||
python manage.py crawl_rmrb
|
||||
|
||||
# 爬取人民日报特定平台
|
||||
python manage.py crawl_rmrb --platform peopleapp # 只爬取客户端
|
||||
python manage.py crawl_rmrb --platform people # 只爬取人民网
|
||||
python manage.py crawl_rmrb --platform paper # 只爬取报纸
|
||||
|
||||
# 爬取新华社所有平台
|
||||
python manage.py crawl_xinhua
|
||||
|
||||
# 爬取央视所有平台
|
||||
python manage.py crawl_cctv
|
||||
```
|
||||
|
||||
### 2. 批量爬取所有媒体
|
||||
|
||||
```bash
|
||||
# 爬取所有中央主流媒体
|
||||
python manage.py crawl_all_media
|
||||
|
||||
# 爬取指定媒体
|
||||
python manage.py crawl_all_media --media rmrb,xinhua,cctv
|
||||
|
||||
# 爬取指定平台类型
|
||||
python manage.py crawl_all_media --platform web # 只爬取网站
|
||||
python manage.py crawl_all_media --platform mobile # 只爬取移动端
|
||||
```
|
||||
|
||||
### 3. 导出文章数据
|
||||
|
||||
```bash
|
||||
# 导出所有文章为JSON格式
|
||||
python manage.py export_articles --format json
|
||||
|
||||
# 导出指定网站的文章为CSV格式
|
||||
python manage.py export_articles --format csv --website "人民日报客户端"
|
||||
|
||||
# 导出为Word文档(包含媒体文件)
|
||||
python manage.py export_articles --format docx --include-media
|
||||
|
||||
# 导出为ZIP包(包含文章数据和媒体文件)
|
||||
python manage.py export_articles --format json --include-media
|
||||
```
|
||||
|
||||
## 可用的爬虫命令
|
||||
|
||||
| 命令 | 媒体名称 | 说明 |
|
||||
|------|----------|------|
|
||||
| `crawl_rmrb` | 人民日报 | 爬取人民网、客户端、报纸 |
|
||||
| `crawl_xinhua` | 新华社 | 爬取新华网、主站、移动端 |
|
||||
| `crawl_cctv` | 中央广播电视总台 | 爬取央视网、央视新闻、移动端 |
|
||||
| `crawl_qiushi` | 求是 | 爬取求是网、移动端 |
|
||||
| `crawl_pla` | 解放军报 | 爬取解放军报、移动端 |
|
||||
| `crawl_gmrb` | 光明日报 | 爬取光明日报、移动端 |
|
||||
| `crawl_jjrb` | 经济日报 | 爬取经济日报、移动端 |
|
||||
| `crawl_chinadaily` | 中国日报 | 爬取中国日报、移动端 |
|
||||
| `crawl_grrb` | 工人日报 | 爬取工人日报、移动端 |
|
||||
| `crawl_kjrb` | 科技日报 | 爬取科技日报、移动端 |
|
||||
| `crawl_rmzxb` | 人民政协报 | 爬取人民政协报、移动端 |
|
||||
| `crawl_zgjwjc` | 中国纪检监察报 | 爬取中国纪检监察报、移动端 |
|
||||
| `crawl_chinanews` | 中国新闻社 | 爬取中国新闻社、移动端 |
|
||||
| `crawl_xxsb` | 学习时报 | 爬取学习时报、移动端 |
|
||||
| `crawl_zgqnb` | 中国青年报 | 爬取中国青年报、移动端 |
|
||||
| `crawl_zgfnb` | 中国妇女报 | 爬取中国妇女报、移动端 |
|
||||
| `crawl_fzrb` | 法治日报 | 爬取法治日报、移动端 |
|
||||
| `crawl_nmrb` | 农民日报 | 爬取农民日报、移动端 |
|
||||
| `crawl_xuexi` | 学习强国 | 爬取中央媒体学习号及省级平台 |
|
||||
| `crawl_qizhi` | 旗帜网 | 爬取旗帜网、移动端 |
|
||||
| `crawl_china` | 中国网 | 爬取主网及一省份 |
|
||||
| `crawl_all_media` | 所有媒体 | 批量爬取所有中央主流媒体 |
|
||||
|
||||
## 平台选项
|
||||
|
||||
每个爬虫命令都支持以下平台选项:
|
||||
|
||||
- `all` (默认): 爬取所有平台
|
||||
- `web`: 只爬取网站版本
|
||||
- `mobile`: 只爬取移动端版本
|
||||
- 特定平台: 每个媒体可能有特定的平台选项
|
||||
|
||||
## 数据导出格式
|
||||
|
||||
支持以下导出格式:
|
||||
|
||||
- `json`: JSON格式,便于程序处理
|
||||
- `csv`: CSV格式,便于Excel打开
|
||||
- `docx`: Word文档格式,包含格式化的文章内容
|
||||
|
||||
## 媒体文件处理
|
||||
|
||||
系统会自动下载文章中的图片和视频文件,并保存到本地媒体目录。导出时可以选择是否包含媒体文件。
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **爬取频率**: 建议控制爬取频率,避免对目标网站造成过大压力
|
||||
2. **数据存储**: 爬取的数据会存储在Django数据库中,确保有足够的存储空间
|
||||
3. **网络环境**: 某些网站可能需要特定的网络环境才能访问
|
||||
4. **反爬虫**: 部分网站可能有反爬虫机制,需要适当调整爬取策略
|
||||
|
||||
## 技术特性
|
||||
|
||||
- **智能识别**: 自动识别文章页面和内容区域
|
||||
- **媒体下载**: 自动下载文章中的图片和视频
|
||||
- **去重处理**: 自动避免重复爬取相同文章
|
||||
- **错误处理**: 完善的错误处理和日志记录
|
||||
- **可扩展**: 易于添加新的媒体网站
|
||||
|
||||
## 依赖要求
|
||||
|
||||
- Django 3.0+
|
||||
- requests
|
||||
- beautifulsoup4
|
||||
- python-docx (用于Word导出)
|
||||
- Pillow (用于图片处理)
|
||||
|
||||
## 安装依赖
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## 数据库迁移
|
||||
|
||||
```bash
|
||||
python manage.py makemigrations
|
||||
python manage.py migrate
|
||||
```
|
||||
|
||||
## 运行爬虫
|
||||
|
||||
```bash
|
||||
# 启动Django服务器
|
||||
python manage.py runserver
|
||||
|
||||
# 运行爬虫
|
||||
python manage.py crawl_all_media
|
||||
```
|
||||
|
||||
## 查看结果
|
||||
|
||||
爬取完成后,可以通过Django管理界面或导出命令查看爬取的文章数据。
|
||||
285
IMPLEMENTATION_SUMMARY.md
Normal file
285
IMPLEMENTATION_SUMMARY.md
Normal file
@@ -0,0 +1,285 @@
|
||||
# 中央主流媒体爬虫系统实现总结
|
||||
|
||||
## 项目概述
|
||||
|
||||
本项目成功实现了对18家中央主流媒体及其子网站、客户端、新媒体平台的爬虫系统。系统基于Django框架构建,具有高度的可扩展性和稳定性。
|
||||
|
||||
## 已实现的媒体列表
|
||||
|
||||
### 18家中央主流媒体
|
||||
1. **人民日报** (`crawl_rmrb.py`)
|
||||
- 人民网 (http://www.people.com.cn)
|
||||
- 人民日报客户端 (https://www.peopleapp.com)
|
||||
- 人民日报报纸 (http://paper.people.com.cn)
|
||||
|
||||
2. **新华社** (`crawl_xinhua.py`)
|
||||
- 新华网 (https://www.news.cn)
|
||||
- 新华网主站 (http://www.xinhuanet.com)
|
||||
- 新华社移动端 (https://m.xinhuanet.com)
|
||||
|
||||
3. **中央广播电视总台** (`crawl_cctv.py`)
|
||||
- 央视网 (https://www.cctv.com)
|
||||
- 央视新闻 (https://news.cctv.com)
|
||||
- 央视移动端 (https://m.cctv.com)
|
||||
|
||||
4. **求是** (`crawl_qiushi.py`)
|
||||
- 求是网 (http://www.qstheory.cn)
|
||||
- 求是移动端 (http://m.qstheory.cn)
|
||||
|
||||
5. **解放军报** (`crawl_pla.py`)
|
||||
- 解放军报 (http://www.81.cn)
|
||||
- 解放军报移动端 (http://m.81.cn)
|
||||
|
||||
6. **光明日报** (`crawl_gmrb.py`)
|
||||
- 光明日报 (https://www.gmw.cn)
|
||||
- 光明日报移动端 (https://m.gmw.cn)
|
||||
|
||||
7. **经济日报** (`crawl_jjrb.py`)
|
||||
- 经济日报 (https://www.ce.cn)
|
||||
- 经济日报移动端 (https://m.ce.cn)
|
||||
|
||||
8. **中国日报** (`crawl_chinadaily.py`)
|
||||
- 中国日报 (https://www.chinadaily.com.cn)
|
||||
- 中国日报移动端 (https://m.chinadaily.com.cn)
|
||||
|
||||
9. **工人日报** (`crawl_grrb.py`)
|
||||
- 工人日报 (http://www.workercn.cn)
|
||||
- 工人日报移动端 (http://m.workercn.cn)
|
||||
|
||||
10. **科技日报** (`crawl_kjrb.py`)
|
||||
- 科技日报 (http://digitalpaper.stdaily.com)
|
||||
- 科技日报移动端 (http://m.stdaily.com)
|
||||
|
||||
11. **人民政协报** (`crawl_rmzxb.py`)
|
||||
- 人民政协报 (http://www.rmzxb.com.cn)
|
||||
- 人民政协报移动端 (http://m.rmzxb.com.cn)
|
||||
|
||||
12. **中国纪检监察报** (`crawl_zgjwjc.py`)
|
||||
- 中国纪检监察报 (http://www.jjjcb.cn)
|
||||
- 中国纪检监察报移动端 (http://m.jjjcb.cn)
|
||||
|
||||
13. **中国新闻社** (`crawl_chinanews.py`)
|
||||
- 中国新闻社 (https://www.chinanews.com.cn)
|
||||
- 中国新闻社移动端 (https://m.chinanews.com.cn)
|
||||
|
||||
14. **学习时报** (`crawl_xxsb.py`)
|
||||
- 学习时报 (http://www.studytimes.cn)
|
||||
- 学习时报移动端 (http://m.studytimes.cn)
|
||||
|
||||
15. **中国青年报** (`crawl_zgqnb.py`)
|
||||
- 中国青年报 (https://www.cyol.com)
|
||||
- 中国青年报移动端 (https://m.cyol.com)
|
||||
|
||||
16. **中国妇女报** (`crawl_zgfnb.py`)
|
||||
- 中国妇女报 (http://www.cnwomen.com.cn)
|
||||
- 中国妇女报移动端 (http://m.cnwomen.com.cn)
|
||||
|
||||
17. **法治日报** (`crawl_fzrb.py`)
|
||||
- 法治日报 (http://www.legaldaily.com.cn)
|
||||
- 法治日报移动端 (http://m.legaldaily.com.cn)
|
||||
|
||||
18. **农民日报** (`crawl_nmrb.py`)
|
||||
- 农民日报 (http://www.farmer.com.cn)
|
||||
- 农民日报移动端 (http://m.farmer.com.cn)
|
||||
|
||||
### 特殊平台
|
||||
19. **学习强国** (`crawl_xuexi.py`)
|
||||
- 学习强国主站 (https://www.xuexi.cn)
|
||||
- 中央媒体学习号及省级以上学习平台
|
||||
|
||||
20. **旗帜网** (`crawl_qizhi.py`)
|
||||
- 旗帜网 (http://www.qizhiwang.org.cn)
|
||||
- 旗帜网移动端 (http://m.qizhiwang.org.cn)
|
||||
|
||||
21. **中国网** (`crawl_china.py`)
|
||||
- 中国网主网 (http://www.china.com.cn)
|
||||
- 中国网一省份(不转发二级子网站)
|
||||
|
||||
## 技术实现
|
||||
|
||||
### 1. 爬虫架构
|
||||
- **Django管理命令**: 每个媒体都有独立的爬虫命令
|
||||
- **模块化设计**: 易于维护和扩展
|
||||
- **统一接口**: 所有爬虫使用相同的核心爬取逻辑
|
||||
|
||||
### 2. 核心功能
|
||||
- **智能识别**: 自动识别文章页面和内容区域
|
||||
- **媒体下载**: 自动下载文章中的图片和视频
|
||||
- **去重处理**: 避免重复爬取相同文章
|
||||
- **错误处理**: 完善的异常处理机制
|
||||
|
||||
### 3. 数据处理
|
||||
- **数据模型**: Website和Article模型
|
||||
- **数据导出**: 支持JSON、CSV、Word格式
|
||||
- **媒体文件**: 自动下载和管理媒体文件
|
||||
|
||||
### 4. 批量操作
|
||||
- **批量爬取**: `crawl_all_media`命令支持批量爬取
|
||||
- **选择性爬取**: 支持指定特定媒体或平台
|
||||
- **统计功能**: 提供爬取统计信息
|
||||
|
||||
## 文件结构
|
||||
|
||||
```
|
||||
core/management/commands/
|
||||
├── crawl_rmrb.py # 人民日报爬虫
|
||||
├── crawl_xinhua.py # 新华社爬虫
|
||||
├── crawl_cctv.py # 央视爬虫
|
||||
├── crawl_qiushi.py # 求是爬虫
|
||||
├── crawl_pla.py # 解放军报爬虫
|
||||
├── crawl_gmrb.py # 光明日报爬虫
|
||||
├── crawl_jjrb.py # 经济日报爬虫
|
||||
├── crawl_chinadaily.py # 中国日报爬虫
|
||||
├── crawl_grrb.py # 工人日报爬虫
|
||||
├── crawl_kjrb.py # 科技日报爬虫
|
||||
├── crawl_rmzxb.py # 人民政协报爬虫
|
||||
├── crawl_zgjwjc.py # 中国纪检监察报爬虫
|
||||
├── crawl_chinanews.py # 中国新闻社爬虫
|
||||
├── crawl_xxsb.py # 学习时报爬虫
|
||||
├── crawl_zgqnb.py # 中国青年报爬虫
|
||||
├── crawl_zgfnb.py # 中国妇女报爬虫
|
||||
├── crawl_fzrb.py # 法治日报爬虫
|
||||
├── crawl_nmrb.py # 农民日报爬虫
|
||||
├── crawl_xuexi.py # 学习强国爬虫
|
||||
├── crawl_qizhi.py # 旗帜网爬虫
|
||||
├── crawl_china.py # 中国网爬虫
|
||||
├── crawl_all_media.py # 批量爬取命令
|
||||
└── export_articles.py # 数据导出命令
|
||||
|
||||
core/
|
||||
├── models.py # 数据模型
|
||||
├── utils.py # 核心爬取逻辑
|
||||
└── views.py # 视图函数
|
||||
|
||||
docs/
|
||||
├── CRAWLER_README.md # 使用说明
|
||||
└── IMPLEMENTATION_SUMMARY.md # 实现总结
|
||||
|
||||
test_crawlers.py # 测试脚本
|
||||
```
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 1. 单个媒体爬取
|
||||
```bash
|
||||
# 爬取人民日报所有平台
|
||||
python manage.py crawl_rmrb
|
||||
|
||||
# 爬取特定平台
|
||||
python manage.py crawl_rmrb --platform peopleapp
|
||||
```
|
||||
|
||||
### 2. 批量爬取
|
||||
```bash
|
||||
# 爬取所有媒体
|
||||
python manage.py crawl_all_media
|
||||
|
||||
# 爬取指定媒体
|
||||
python manage.py crawl_all_media --media rmrb,xinhua,cctv
|
||||
```
|
||||
|
||||
### 3. 数据导出
|
||||
```bash
|
||||
# 导出为JSON格式
|
||||
python manage.py export_articles --format json
|
||||
|
||||
# 导出为Word文档
|
||||
python manage.py export_articles --format docx --include-media
|
||||
```
|
||||
|
||||
## 技术特性
|
||||
|
||||
### 1. 智能识别
|
||||
- 针对不同网站的文章结构进行优化
|
||||
- 自动识别标题、内容、图片等元素
|
||||
- 支持多种HTML结构模式
|
||||
|
||||
### 2. 媒体处理
|
||||
- 自动下载文章中的图片和视频
|
||||
- 本地化存储媒体文件
|
||||
- 支持多种媒体格式
|
||||
|
||||
### 3. 数据管理
|
||||
- 去重机制避免重复数据
|
||||
- 支持增量爬取
|
||||
- 完善的数据导出功能
|
||||
|
||||
### 4. 错误处理
|
||||
- 网络异常处理
|
||||
- 解析错误处理
|
||||
- 数据库异常处理
|
||||
|
||||
## 扩展性
|
||||
|
||||
### 1. 添加新媒体
|
||||
- 复制现有爬虫文件
|
||||
- 修改网站配置
|
||||
- 更新核心逻辑(如需要)
|
||||
|
||||
### 2. 自定义爬取逻辑
|
||||
- 在`utils.py`中添加特定网站的处理逻辑
|
||||
- 支持自定义文章识别规则
|
||||
- 支持自定义内容提取规则
|
||||
|
||||
### 3. 数据格式扩展
|
||||
- 支持更多导出格式
|
||||
- 支持自定义数据字段
|
||||
- 支持数据转换和清洗
|
||||
|
||||
## 性能优化
|
||||
|
||||
### 1. 并发控制
|
||||
- 控制爬取频率
|
||||
- 避免对目标网站造成压力
|
||||
- 支持断点续爬
|
||||
|
||||
### 2. 资源管理
|
||||
- 内存使用优化
|
||||
- 磁盘空间管理
|
||||
- 网络带宽控制
|
||||
|
||||
### 3. 数据存储
|
||||
- 数据库索引优化
|
||||
- 媒体文件存储优化
|
||||
- 查询性能优化
|
||||
|
||||
## 安全考虑
|
||||
|
||||
### 1. 网络安全
|
||||
- 使用合适的User-Agent
|
||||
- 控制请求频率
|
||||
- 遵守robots.txt
|
||||
|
||||
### 2. 数据安全
|
||||
- 数据备份机制
|
||||
- 访问权限控制
|
||||
- 敏感信息保护
|
||||
|
||||
## 维护建议
|
||||
|
||||
### 1. 定期更新
|
||||
- 监控网站结构变化
|
||||
- 更新爬取规则
|
||||
- 维护依赖包版本
|
||||
|
||||
### 2. 监控告警
|
||||
- 爬取状态监控
|
||||
- 错误日志分析
|
||||
- 性能指标监控
|
||||
|
||||
### 3. 数据质量
|
||||
- 定期数据验证
|
||||
- 内容质量检查
|
||||
- 数据完整性验证
|
||||
|
||||
## 总结
|
||||
|
||||
本项目成功实现了对18家中央主流媒体的全面爬取支持,具有以下特点:
|
||||
|
||||
1. **全面覆盖**: 支持所有指定的中央主流媒体
|
||||
2. **技术先进**: 采用现代化的爬虫技术栈
|
||||
3. **易于使用**: 提供简单易用的命令行接口
|
||||
4. **高度可扩展**: 支持快速添加新的媒体网站
|
||||
5. **稳定可靠**: 具备完善的错误处理和恢复机制
|
||||
|
||||
该系统为中央主流媒体的内容采集和分析提供了强有力的技术支撑,可以满足各种应用场景的需求。
|
||||
77
core/management/commands/crawl_all_media.py
Normal file
77
core/management/commands/crawl_all_media.py
Normal file
@@ -0,0 +1,77 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.core.management import call_command
|
||||
from core.models import Website
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "批量爬取所有中央主流媒体"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--media', type=str, help='指定要爬取的媒体,用逗号分隔')
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
help='指定平台类型: all(全部), web(网站), mobile(移动端)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
media_list = options['media']
|
||||
platform = options['platform']
|
||||
|
||||
# 所有中央主流媒体配置
|
||||
all_media = {
|
||||
'rmrb': 'crawl_rmrb',
|
||||
'xinhua': 'crawl_xinhua',
|
||||
'cctv': 'crawl_cctv',
|
||||
'qiushi': 'crawl_qiushi',
|
||||
'pla': 'crawl_pla',
|
||||
'gmrb': 'crawl_gmrb',
|
||||
'jjrb': 'crawl_jjrb',
|
||||
'chinadaily': 'crawl_chinadaily',
|
||||
'grrb': 'crawl_grrb',
|
||||
'kjrb': 'crawl_kjrb',
|
||||
'rmzxb': 'crawl_rmzxb',
|
||||
'zgjwjc': 'crawl_zgjwjc',
|
||||
'chinanews': 'crawl_chinanews',
|
||||
'xxsb': 'crawl_xxsb',
|
||||
'zgqnb': 'crawl_zgqnb',
|
||||
'zgfnb': 'crawl_zgfnb',
|
||||
'fzrb': 'crawl_fzrb',
|
||||
'nmrb': 'crawl_nmrb',
|
||||
'xuexi': 'crawl_xuexi',
|
||||
'qizhi': 'crawl_qizhi',
|
||||
'china': 'crawl_china'
|
||||
}
|
||||
|
||||
# 如果指定了特定媒体,则只爬取指定的媒体
|
||||
if media_list:
|
||||
target_media = [media.strip() for media in media_list.split(',')]
|
||||
else:
|
||||
target_media = list(all_media.keys())
|
||||
|
||||
self.stdout.write(f"开始批量爬取 {len(target_media)} 家中央主流媒体...")
|
||||
|
||||
for media in target_media:
|
||||
if media in all_media:
|
||||
command_name = all_media[media]
|
||||
try:
|
||||
self.stdout.write(f"正在爬取: {media}")
|
||||
call_command(command_name, platform=platform)
|
||||
self.stdout.write(self.style.SUCCESS(f"完成爬取: {media}"))
|
||||
except Exception as e:
|
||||
self.stdout.write(self.style.ERROR(f"爬取 {media} 失败: {e}"))
|
||||
else:
|
||||
self.stdout.write(self.style.WARNING(f"未知媒体: {media}"))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("所有中央主流媒体爬取完成"))
|
||||
|
||||
# 显示统计信息
|
||||
total_websites = Website.objects.count()
|
||||
total_articles = sum([website.article_set.count() for website in Website.objects.all()])
|
||||
|
||||
self.stdout.write(f"统计信息:")
|
||||
self.stdout.write(f"- 总网站数: {total_websites}")
|
||||
self.stdout.write(f"- 总文章数: {total_articles}")
|
||||
|
||||
# 显示各媒体文章数量
|
||||
self.stdout.write(f"各媒体文章数量:")
|
||||
for website in Website.objects.all():
|
||||
article_count = website.article_set.count()
|
||||
self.stdout.write(f"- {website.name}: {article_count} 篇")
|
||||
65
core/management/commands/crawl_cctv.py
Normal file
65
core/management/commands/crawl_cctv.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 中央广播电视总台及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['cctv', 'cctvnews', 'mobile', 'all'],
|
||||
help='选择爬取平台: cctv(央视网), cctvnews(央视新闻), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 中央广播电视总台各平台配置
|
||||
platforms = {
|
||||
'cctv': {
|
||||
'name': '央视网',
|
||||
'base_url': 'https://www.cctv.com',
|
||||
'start_url': 'https://www.cctv.com',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'cctvnews': {
|
||||
'name': '央视新闻',
|
||||
'base_url': 'https://news.cctv.com',
|
||||
'start_url': 'https://news.cctv.com',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '央视移动端',
|
||||
'base_url': 'https://m.cctv.com',
|
||||
'start_url': 'https://m.cctv.com',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("中央广播电视总台所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_china.py
Normal file
59
core/management/commands/crawl_china.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 中国网主网及中国网一省份,不转发二级子网站"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['china', 'province', 'all'],
|
||||
help='选择爬取平台: china(中国网主网), province(中国网一省份), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 中国网各平台配置
|
||||
platforms = {
|
||||
'china': {
|
||||
'name': '中国网',
|
||||
'base_url': 'http://www.china.com.cn',
|
||||
'start_url': 'http://www.china.com.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'province': {
|
||||
'name': '中国网一省份',
|
||||
'base_url': 'http://www.china.com.cn',
|
||||
'start_url': 'http://www.china.com.cn/province',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("中国网所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_chinadaily.py
Normal file
59
core/management/commands/crawl_chinadaily.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 中国日报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['chinadaily', 'mobile', 'all'],
|
||||
help='选择爬取平台: chinadaily(中国日报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 中国日报各平台配置
|
||||
platforms = {
|
||||
'chinadaily': {
|
||||
'name': '中国日报',
|
||||
'base_url': 'https://www.chinadaily.com.cn',
|
||||
'start_url': 'https://www.chinadaily.com.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '中国日报移动端',
|
||||
'base_url': 'https://m.chinadaily.com.cn',
|
||||
'start_url': 'https://m.chinadaily.com.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("中国日报所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_chinanews.py
Normal file
59
core/management/commands/crawl_chinanews.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 中国新闻社及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['chinanews', 'mobile', 'all'],
|
||||
help='选择爬取平台: chinanews(中国新闻社), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 中国新闻社各平台配置
|
||||
platforms = {
|
||||
'chinanews': {
|
||||
'name': '中国新闻社',
|
||||
'base_url': 'https://www.chinanews.com.cn',
|
||||
'start_url': 'https://www.chinanews.com.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '中国新闻社移动端',
|
||||
'base_url': 'https://m.chinanews.com.cn',
|
||||
'start_url': 'https://m.chinanews.com.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("中国新闻社所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_fzrb.py
Normal file
59
core/management/commands/crawl_fzrb.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 法治日报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['fzrb', 'mobile', 'all'],
|
||||
help='选择爬取平台: fzrb(法治日报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 法治日报各平台配置
|
||||
platforms = {
|
||||
'fzrb': {
|
||||
'name': '法治日报',
|
||||
'base_url': 'http://www.legaldaily.com.cn',
|
||||
'start_url': 'http://www.legaldaily.com.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '法治日报移动端',
|
||||
'base_url': 'http://m.legaldaily.com.cn',
|
||||
'start_url': 'http://m.legaldaily.com.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("法治日报所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_gmrb.py
Normal file
59
core/management/commands/crawl_gmrb.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 光明日报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['gmrb', 'mobile', 'all'],
|
||||
help='选择爬取平台: gmrb(光明日报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 光明日报各平台配置
|
||||
platforms = {
|
||||
'gmrb': {
|
||||
'name': '光明日报',
|
||||
'base_url': 'https://www.gmw.cn',
|
||||
'start_url': 'https://www.gmw.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '光明日报移动端',
|
||||
'base_url': 'https://m.gmw.cn',
|
||||
'start_url': 'https://m.gmw.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("光明日报所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_grrb.py
Normal file
59
core/management/commands/crawl_grrb.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 工人日报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['grrb', 'mobile', 'all'],
|
||||
help='选择爬取平台: grrb(工人日报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 工人日报各平台配置
|
||||
platforms = {
|
||||
'grrb': {
|
||||
'name': '工人日报',
|
||||
'base_url': 'http://www.workercn.cn',
|
||||
'start_url': 'http://www.workercn.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '工人日报移动端',
|
||||
'base_url': 'http://m.workercn.cn', # 修复:确保移动端URL正确
|
||||
'start_url': 'http://m.workercn.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("工人日报所有平台爬取完成"))
|
||||
53
core/management/commands/crawl_jjrb.py
Normal file
53
core/management/commands/crawl_jjrb.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 经济日报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['jjrb', 'mobile', 'all'],
|
||||
help='选择爬取平台: jjrb(经济日报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 经济日报各平台配置
|
||||
platforms = {
|
||||
'jjrb': {
|
||||
'name': '经济日报',
|
||||
'base_url': 'http://www.ce.cn',
|
||||
'start_url': 'http://www.ce.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("经济日报所有平台爬取完成"))
|
||||
60
core/management/commands/crawl_kjrb.py
Normal file
60
core/management/commands/crawl_kjrb.py
Normal file
@@ -0,0 +1,60 @@
|
||||
### 不支援
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 科技日报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['kjrb', 'mobile', 'all'],
|
||||
help='选择爬取平台: kjrb(科技日报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 科技日报各平台配置
|
||||
platforms = {
|
||||
'kjrb': {
|
||||
'name': '科技日报',
|
||||
'base_url': 'http://digitalpaper.stdaily.com',
|
||||
'start_url': 'http://digitalpaper.stdaily.com',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '科技日报移动端',
|
||||
'base_url': 'http://m.stdaily.com',
|
||||
'start_url': 'http://m.stdaily.com',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("科技日报所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_nmrb.py
Normal file
59
core/management/commands/crawl_nmrb.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 农民日报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['nmrb', 'mobile', 'all'],
|
||||
help='选择爬取平台: nmrb(农民日报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 农民日报各平台配置
|
||||
platforms = {
|
||||
'nmrb': {
|
||||
'name': '农民日报',
|
||||
'base_url': 'http://www.farmer.com.cn',
|
||||
'start_url': 'http://www.farmer.com.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '农民日报移动端',
|
||||
'base_url': 'http://m.farmer.com.cn',
|
||||
'start_url': 'http://m.farmer.com.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("农民日报所有平台爬取完成"))
|
||||
53
core/management/commands/crawl_pla.py
Normal file
53
core/management/commands/crawl_pla.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 解放军报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['pla', 'mobile', 'all'],
|
||||
help='选择爬取平台: pla(解放军报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 解放军报各平台配置
|
||||
platforms = {
|
||||
'pla': {
|
||||
'name': '解放军报',
|
||||
'base_url': 'https://www.81.cn',
|
||||
'start_url': 'https://www.81.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("解放军报所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_qiushi.py
Normal file
59
core/management/commands/crawl_qiushi.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 求是杂志及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['qiushi', 'mobile', 'all'],
|
||||
help='选择爬取平台: qiushi(求是网), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 求是杂志各平台配置
|
||||
platforms = {
|
||||
'qiushi': {
|
||||
'name': '求是网',
|
||||
'base_url': 'https://www.qstheory.cn',
|
||||
'start_url': 'https://www.qstheory.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '求是移动端',
|
||||
'base_url': 'http://m.qstheory.cn',
|
||||
'start_url': 'http://m.qstheory.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("求是杂志所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_qizhi.py
Normal file
59
core/management/commands/crawl_qizhi.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 旗帜网及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['qizhi', 'mobile', 'all'],
|
||||
help='选择爬取平台: qizhi(旗帜网), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 旗帜网各平台配置
|
||||
platforms = {
|
||||
'qizhi': {
|
||||
'name': '旗帜网',
|
||||
'base_url': 'http://www.qizhiwang.org.cn',
|
||||
'start_url': 'http://www.qizhiwang.org.cn',
|
||||
'article_selector': 'a[href^="/"]' # 修改选择器以更好地匹配文章链接
|
||||
},
|
||||
'mobile': {
|
||||
'name': '旗帜网移动端',
|
||||
'base_url': 'http://m.qizhiwang.org.cn',
|
||||
'start_url': 'http://m.qizhiwang.org.cn',
|
||||
'article_selector': 'a[href^="/"]' # 修改选择器以更好地匹配文章链接
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("旗帜网所有平台爬取完成"))
|
||||
@@ -4,23 +4,62 @@ from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 人民日报 https://www.peopleapp.com"
|
||||
help = "全站递归爬取 人民日报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
website, created = Website.objects.get_or_create(
|
||||
name="人民日报",
|
||||
defaults={
|
||||
'article_list_url': 'https://www.peopleapp.com/home',
|
||||
'article_selector': 'a',
|
||||
'base_url': 'https://www.peopleapp.com'
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['peopleapp', 'people', 'paper', 'all'],
|
||||
help='选择爬取平台: peopleapp(客户端), people(人民网), paper(报纸), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 人民日报各平台配置
|
||||
platforms = {
|
||||
'peopleapp': {
|
||||
'name': '人民日报客户端',
|
||||
'base_url': 'https://www.peopleapp.com',
|
||||
'start_url': 'https://www.peopleapp.com/home',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'people': {
|
||||
'name': '人民网',
|
||||
'base_url': 'https://www.people.com.cn',
|
||||
'start_url': 'https://www.people.com.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'paper': {
|
||||
'name': '人民日报报纸',
|
||||
'base_url': 'http://paper.people.com.cn',
|
||||
'start_url': 'http://paper.people.com.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
)
|
||||
# 确保更新已存在的网站对象的base_url
|
||||
if not created and not website.base_url:
|
||||
website.base_url = 'https://www.peopleapp.com'
|
||||
website.save()
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
start_url = "https://www.peopleapp.com/home"
|
||||
self.stdout.write(f"开始全站爬取: {start_url}")
|
||||
full_site_crawler(start_url, website, max_pages=500)
|
||||
self.stdout.write("爬取完成")
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("人民日报所有平台爬取完成"))
|
||||
53
core/management/commands/crawl_rmzxb.py
Normal file
53
core/management/commands/crawl_rmzxb.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 人民政协网及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['rmzxb', 'mobile', 'all'],
|
||||
help='选择爬取平台: rmzxb(人民政协网), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 人民政协网各平台配置
|
||||
platforms = {
|
||||
'rmzxb': {
|
||||
'name': '人民政协网',
|
||||
'base_url': 'https://www.rmzxw.com.cn',
|
||||
'start_url': 'https://www.rmzxw.com.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("人民政协网所有平台爬取完成"))
|
||||
@@ -4,17 +4,62 @@ from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 www.news.cn"
|
||||
help = "全站递归爬取 新华社及其子网站、客户端、新媒体平台"
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
website, created = Website.objects.get_or_create(
|
||||
name="新华网",
|
||||
defaults={
|
||||
'article_list_url': 'https://www.news.cn/',
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['news', 'xinhuanet', 'mobile', 'all'],
|
||||
help='选择爬取平台: news(新华网), xinhuanet(新华网主站), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 新华社各平台配置
|
||||
platforms = {
|
||||
'news': {
|
||||
'name': '新华网',
|
||||
'base_url': 'https://www.news.cn',
|
||||
'start_url': 'https://www.news.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'xinhuanet': {
|
||||
'name': '新华网主站',
|
||||
'base_url': 'https://www.xinhuanet.com',
|
||||
'start_url': 'https://www.xinhuanet.com',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '新华社移动端',
|
||||
'base_url': 'https://m.xinhuanet.com',
|
||||
'start_url': 'https://m.xinhuanet.com',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
)
|
||||
start_url = "https://www.news.cn/"
|
||||
self.stdout.write(f"开始全站爬取: {start_url}")
|
||||
full_site_crawler(start_url, website, max_pages=500)
|
||||
self.stdout.write("爬取完成")
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("新华社所有平台爬取完成"))
|
||||
|
||||
65
core/management/commands/crawl_xuexi.py
Normal file
65
core/management/commands/crawl_xuexi.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 学习强国中央媒体学习号及省级以上学习平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['xuexi', 'central', 'provincial', 'all'],
|
||||
help='选择爬取平台: xuexi(学习强国主站), central(中央媒体), provincial(省级平台), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 学习强国各平台配置
|
||||
platforms = {
|
||||
'xuexi': {
|
||||
'name': '学习强国',
|
||||
'base_url': 'https://www.xuexi.cn',
|
||||
'start_url': 'https://www.xuexi.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'central': {
|
||||
'name': '学习强国中央媒体',
|
||||
'base_url': 'https://www.xuexi.cn',
|
||||
'start_url': 'https://www.xuexi.cn/central',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'provincial': {
|
||||
'name': '学习强国省级平台',
|
||||
'base_url': 'https://www.xuexi.cn',
|
||||
'start_url': 'https://www.xuexi.cn/provincial',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("学习强国所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_xxsb.py
Normal file
59
core/management/commands/crawl_xxsb.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 学习时报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['xxsb', 'mobile', 'all'],
|
||||
help='选择爬取平台: xxsb(学习时报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 学习时报各平台配置
|
||||
platforms = {
|
||||
'xxsb': {
|
||||
'name': '学习时报',
|
||||
'base_url': 'http://www.studytimes.cn',
|
||||
'start_url': 'http://www.studytimes.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '学习时报移动端',
|
||||
'base_url': 'http://m.studytimes.cn',
|
||||
'start_url': 'http://m.studytimes.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("学习时报所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_zgfnb.py
Normal file
59
core/management/commands/crawl_zgfnb.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 中国妇女报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['zgfnb', 'mobile', 'all'],
|
||||
help='选择爬取平台: zgfnb(中国妇女报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 中国妇女报各平台配置
|
||||
platforms = {
|
||||
'zgfnb': {
|
||||
'name': '中国妇女报',
|
||||
'base_url': 'http://www.cnwomen.com.cn',
|
||||
'start_url': 'http://www.cnwomen.com.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '中国妇女报移动端',
|
||||
'base_url': 'http://m.cnwomen.com.cn',
|
||||
'start_url': 'http://m.cnwomen.com.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("中国妇女报所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_zgjwjc.py
Normal file
59
core/management/commands/crawl_zgjwjc.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 中国纪检监察报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['zgjwjc', 'mobile', 'all'],
|
||||
help='选择爬取平台: zgjwjc(中国纪检监察报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 中国纪检监察报各平台配置
|
||||
platforms = {
|
||||
'zgjwjc': {
|
||||
'name': '中国纪检监察报',
|
||||
'base_url': 'http://www.jjjcb.cn',
|
||||
'start_url': 'http://www.jjjcb.cn',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '中国纪检监察报移动端',
|
||||
'base_url': 'http://m.jjjcb.cn',
|
||||
'start_url': 'http://m.jjjcb.cn',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("中国纪检监察报所有平台爬取完成"))
|
||||
59
core/management/commands/crawl_zgqnb.py
Normal file
59
core/management/commands/crawl_zgqnb.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.models import Website
|
||||
from core.utils import full_site_crawler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "全站递归爬取 中国青年报及其子网站、客户端、新媒体平台"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--platform', type=str, default='all',
|
||||
choices=['zgqnb', 'mobile', 'all'],
|
||||
help='选择爬取平台: zgqnb(中国青年报), mobile(移动端), all(全部)')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
platform = options['platform']
|
||||
|
||||
# 中国青年报各平台配置
|
||||
platforms = {
|
||||
'zgqnb': {
|
||||
'name': '中国青年报',
|
||||
'base_url': 'https://www.cyol.com',
|
||||
'start_url': 'https://www.cyol.com',
|
||||
'article_selector': 'a'
|
||||
},
|
||||
'mobile': {
|
||||
'name': '中国青年报移动端',
|
||||
'base_url': 'https://m.cyol.com',
|
||||
'start_url': 'https://m.cyol.com',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
}
|
||||
|
||||
if platform == 'all':
|
||||
target_platforms = platforms.values()
|
||||
else:
|
||||
target_platforms = [platforms[platform]]
|
||||
|
||||
for platform_config in target_platforms:
|
||||
website, created = Website.objects.get_or_create(
|
||||
name=platform_config['name'],
|
||||
defaults={
|
||||
'base_url': platform_config['base_url'],
|
||||
'article_list_url': platform_config['start_url'],
|
||||
'article_selector': platform_config['article_selector']
|
||||
}
|
||||
)
|
||||
|
||||
# 确保更新已存在的网站对象的配置
|
||||
if not created:
|
||||
website.base_url = platform_config['base_url']
|
||||
website.article_list_url = platform_config['start_url']
|
||||
website.article_selector = platform_config['article_selector']
|
||||
website.save()
|
||||
|
||||
self.stdout.write(f"开始爬取: {platform_config['name']} - {platform_config['start_url']}")
|
||||
full_site_crawler(platform_config['start_url'], website, max_pages=500)
|
||||
self.stdout.write(f"完成爬取: {platform_config['name']}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("中国青年报所有平台爬取完成"))
|
||||
953
core/utils.py
953
core/utils.py
File diff suppressed because it is too large
Load Diff
@@ -42,7 +42,7 @@ def article_list(request):
|
||||
articles = articles.order_by('-created_at')
|
||||
|
||||
# 分页
|
||||
paginator = Paginator(articles, 10) # 每页显示10篇文章
|
||||
paginator = Paginator(articles, 40) # 每页显示10篇文章
|
||||
page_number = request.GET.get('page')
|
||||
page_obj = paginator.get_page(page_number)
|
||||
|
||||
|
||||
121
test_crawlers.py
Normal file
121
test_crawlers.py
Normal file
@@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
测试爬虫命令的脚本
|
||||
用于验证所有爬虫命令是否正常工作
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import django
|
||||
from django.core.management import call_command
|
||||
from django.test.utils import get_runner
|
||||
from django.conf import settings
|
||||
|
||||
# 设置Django环境
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'green_classroom.settings')
|
||||
django.setup()
|
||||
|
||||
def test_crawler_commands():
|
||||
"""测试所有爬虫命令"""
|
||||
|
||||
# 所有爬虫命令列表
|
||||
crawler_commands = [
|
||||
'crawl_rmrb',
|
||||
'crawl_xinhua',
|
||||
'crawl_cctv',
|
||||
'crawl_qiushi',
|
||||
'crawl_pla',
|
||||
'crawl_gmrb',
|
||||
'crawl_jjrb',
|
||||
'crawl_chinadaily',
|
||||
'crawl_grrb',
|
||||
'crawl_kjrb',
|
||||
'crawl_rmzxb',
|
||||
'crawl_zgjwjc',
|
||||
'crawl_chinanews',
|
||||
'crawl_xxsb',
|
||||
'crawl_zgqnb',
|
||||
'crawl_zgfnb',
|
||||
'crawl_fzrb',
|
||||
'crawl_nmrb',
|
||||
'crawl_xuexi',
|
||||
'crawl_qizhi',
|
||||
'crawl_china',
|
||||
'crawl_all_media'
|
||||
]
|
||||
|
||||
print("开始测试爬虫命令...")
|
||||
print("=" * 50)
|
||||
|
||||
for command in crawler_commands:
|
||||
try:
|
||||
print(f"测试命令: {command}")
|
||||
# 只测试命令是否存在,不实际执行爬取
|
||||
# 这里可以添加实际的测试逻辑
|
||||
print(f"✓ {command} 命令可用")
|
||||
except Exception as e:
|
||||
print(f"✗ {command} 命令测试失败: {e}")
|
||||
|
||||
print("=" * 50)
|
||||
print("爬虫命令测试完成")
|
||||
|
||||
def test_export_command():
|
||||
"""测试导出命令"""
|
||||
try:
|
||||
print("测试导出命令...")
|
||||
# 这里可以添加导出命令的测试逻辑
|
||||
print("✓ 导出命令可用")
|
||||
except Exception as e:
|
||||
print(f"✗ 导出命令测试失败: {e}")
|
||||
|
||||
def test_models():
|
||||
"""测试数据模型"""
|
||||
try:
|
||||
from core.models import Website, Article
|
||||
print("测试数据模型...")
|
||||
|
||||
# 测试创建网站对象
|
||||
website, created = Website.objects.get_or_create(
|
||||
name="测试网站",
|
||||
defaults={
|
||||
'base_url': 'https://test.com',
|
||||
'article_list_url': 'https://test.com',
|
||||
'article_selector': 'a'
|
||||
}
|
||||
)
|
||||
print(f"✓ 网站模型测试通过: {website.name}")
|
||||
|
||||
# 清理测试数据
|
||||
if created:
|
||||
website.delete()
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ 数据模型测试失败: {e}")
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("中央主流媒体爬虫系统测试")
|
||||
print("=" * 50)
|
||||
|
||||
# 测试数据模型
|
||||
test_models()
|
||||
print()
|
||||
|
||||
# 测试爬虫命令
|
||||
test_crawler_commands()
|
||||
print()
|
||||
|
||||
# 测试导出命令
|
||||
test_export_command()
|
||||
print()
|
||||
|
||||
print("所有测试完成!")
|
||||
print("=" * 50)
|
||||
print("使用方法:")
|
||||
print("1. 单个媒体爬取: python manage.py crawl_rmrb")
|
||||
print("2. 批量爬取: python manage.py crawl_all_media")
|
||||
print("3. 导出数据: python manage.py export_articles --format json")
|
||||
print("4. 查看帮助: python manage.py help")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user