This commit is contained in:
2025-09-24 03:52:55 +08:00
parent 8592833d74
commit 81a17132e2
2 changed files with 161 additions and 3 deletions

152
KEYWORD_CRAWLER_EXAMPLES.md Normal file
View File

@@ -0,0 +1,152 @@
# 关键词爬取功能使用示例
## 快速开始
### 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. **定期清理**
- 定期清理不需要的文章数据
- 清理过期的媒体文件

View File

@@ -82,10 +82,16 @@ class CrawlTask(models.Model):
def get_websites_display(self): def get_websites_display(self):
"""获取网站列表的显示文本""" """获取网站列表的显示文本"""
websites = self.websites.all() try:
if not websites: websites = self.websites.all()
if not websites:
return "所有网站"
# 确保网站名称是字符串并可以被join处理
website_names = [str(w.name) for w in websites if w.name]
return ", ".join(website_names) if website_names else "所有网站"
except Exception:
# 如果出现任何异常,返回默认值
return "所有网站" return "所有网站"
return ", ".join([w.name for w in websites])
def get_duration(self): def get_duration(self):
"""获取任务执行时长""" """获取任务执行时长"""