Files
green_classroom/KEYWORD_CRAWLER_EXAMPLES.md
2025-09-24 03:52:55 +08:00

153 lines
4.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 关键词爬取功能使用示例
## 快速开始
### 1. 查看支持的网站
```bash
python manage.py crawl_by_keyword --list-websites
```
### 2. 基本关键词搜索
```bash
# 搜索"人工智能"相关文章
python manage.py crawl_by_keyword --keyword "人工智能"
# 搜索"两会"相关文章,限制数量
python manage.py crawl_by_keyword --keyword "两会" --max-pages 3 --max-articles 20
```
### 3. 指定网站搜索
```bash
# 只在人民日报和新华网搜索
python manage.py crawl_by_keyword --keyword "人工智能" --websites "人民日报" "新华网"
```
### 4. 日期范围搜索
```bash
# 搜索2024年1月的文章
python manage.py crawl_by_keyword --keyword "新闻" --start-date "2024-01-01" --end-date "2024-01-31"
```
### 5. 历史文章爬取
```bash
# 爬取最近30天的历史文章
python manage.py crawl_by_keyword --keyword "新闻" --historical
# 爬取指定日期范围的历史文章
python manage.py crawl_by_keyword --keyword "新闻" --historical --start-date "2024-01-01" --end-date "2024-01-31"
```
### 6. 保存结果
```bash
# 将爬取结果保存到JSON文件
python manage.py crawl_by_keyword --keyword "人工智能" --output results.json
```
## 多网站一键爬取
### 1. 全站爬取
```bash
# 爬取所有网站的最新文章
python manage.py crawl_all_websites --mode full
# 爬取指定网站
python manage.py crawl_all_websites --mode full --websites "新华网" "人民日报" "央视网"
```
### 2. 关键词爬取
```bash
# 在所有网站搜索"人工智能"
python manage.py crawl_all_websites --mode keyword --keyword "人工智能"
# 限制搜索页数和文章数量
python manage.py crawl_all_websites --mode keyword --keyword "人工智能" --max-search-pages 5 --max-articles 30
```
### 3. 混合模式
```bash
# 同时进行全站爬取和关键词搜索
python manage.py crawl_all_websites --mode both --keyword "人工智能"
```
## 实际使用场景
### 场景1新闻热点追踪
```bash
# 追踪"人工智能"相关新闻
python manage.py crawl_by_keyword --keyword "人工智能" --max-pages 5 --max-articles 50 --output ai_news.json
```
### 场景2政策文件收集
```bash
# 收集"政策"相关文章
python manage.py crawl_by_keyword --keyword "政策" --websites "中国政府网" "新华网" "人民日报" --max-articles 30
```
### 场景3历史资料整理
```bash
# 整理2024年1月的所有新闻
python manage.py crawl_by_keyword --keyword "新闻" --historical --start-date "2024-01-01" --end-date "2024-01-31" --max-articles 100
```
### 场景4全面信息收集
```bash
# 一键收集所有网站的最新信息
python manage.py crawl_all_websites --mode both --keyword "新闻" --max-search-pages 3 --max-articles 20
```
## 注意事项
1. **网络连接**:确保网络连接稳定
2. **请求频率**:系统会自动控制请求频率,避免对目标网站造成压力
3. **存储空间**:爬取的文章和媒体文件会占用存储空间
4. **时间消耗**:大量爬取可能需要较长时间
5. **网站限制**:某些网站可能有反爬虫机制
## 故障排除
### 常见问题
1. **搜索无结果**
- 检查关键词是否正确
- 尝试使用更通用的关键词
- 检查日期范围是否合理
2. **网站访问失败**
- 检查网络连接
- 某些网站可能暂时不可用
- 尝试减少并发请求
3. **编码问题**
- 系统已自动处理常见编码问题
- 如仍有问题,请检查网站编码设置
### 调试技巧
1. **使用小范围测试**
```bash
python manage.py crawl_by_keyword --keyword "测试" --websites "新华网" --max-pages 1 --max-articles 3
```
2. **查看详细输出**
- 命令会显示详细的爬取进度
- 注意错误信息和警告
3. **保存结果分析**
```bash
python manage.py crawl_by_keyword --keyword "测试" --output debug.json
```
## 性能优化建议
1. **合理设置参数**
- 根据需求调整 `max-pages` 和 `max-articles`
- 避免设置过大的数值
2. **分批处理**
- 对于大量数据,建议分批处理
- 可以按网站或时间段分批
3. **定期清理**
- 定期清理不需要的文章数据
- 清理过期的媒体文件