热点抓取技能 (Trend Hunting) v1.0.4
概述
从各大平台抓取热点话题,构建选题库。为 AI 互动剧公司提供时事热点素材。
输入规范
必填参数
platforms: 要抓取的平台列表 (weibo, baidu, zhihu, douyin, bilibili)
category: 热点类别 (entertainment, tech, sports, military, all)
limit: 最大抓取数量 (默认 50)
输入示例
{
"platforms": ["weibo", "baidu", "zhihu"],
"category": "all",
"limit": 50
}
输出规范
JSON Schema
{
"trends": [
{
"title": "string - 热点标题",
"url": "string - 热点链接",
"source": "string - 来源平台 (weibo/baidu/zhihu/douyin/bilibili)",
"hot_value": "number - 热度值",
"timestamp": "string - ISO8601 时间戳",
"category": "string - 分类 (entertainment/tech/sports/military/social)"
}
],
"metadata": {
"total_count": "number - 总数",
"platforms": "array - 抓取平台",
"fetch_time": "string - 抓取时间"
}
}
爬虫配置
平台配置
请求配置
- 超时: 10 秒 (socket timeout)
- 重试: 3 次,每次间隔 2 秒 (指数退避: 2s, 4s, 8s)
- 并发: 最多 3 个平台并行 (asyncio.gather)
- Header 伪装:
python
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
"Accept": "application/json, text/html",
"Accept-Language": "zh-CN,zh;q=0.9",
"Referer": "https://www.bilibili.com"
}
数据清洗规则
- 去除 HTML 标签:
re.sub(r'<[^>]+>', '', text)
- 去除多余空白:
re.sub(r'\s+', ' ', text).strip()
- 去除表情符号:
re.sub(r'\[.*?\]', '', text) (微博表情如 [哈哈])
- URL 规范化:
- 相对路径拼接:
urljoin(base_url, relative_path)
- 短链转换: 使用 API 展开 t.cn, url.cn
- 时间戳标准化: 转为 ISO 8601 格式 (UTC+8)
python
from datetime import datetime, timezone, timedelta
dt = datetime.now(timezone(timedelta(hours=8)))
- 热度值归一化:
- "100万" → 1000000
- "100W" → 1000000
- "热度 1234" → 1234
错误处理
| 错误类型 |
处理方式 |
| 超时 |
重试 3 次,记录 timeout_error |
| 解析失败 |
跳过该条,记录 parse_error |
| 429 限流 |
等待 60 秒后重试 |
| 网络错误 |
返回空列表,记录 network_error |
| 反爬拦截 |
切换 User-Agent,更换代理 |
质量标准
准确性 (40%)
- ✓ JSON 解析无错误
- ✓ 无乱码或特殊字符
- ✓ 去重准确 (title 去重)
完整性 (30%)
- ✓ 字段完整率 100%
- ✓ 每个平台最少抓取 10 条
- ✓ 分类标签正确
效率 (30%)
- ✓ 单平台 ≤10 秒
- ✓ 总耗时 ≤30 秒
- ✓ 内存占用 ≤200MB
- ✓ CPU 占用 ≤50%
评分维度 (Benchmark)
| 维度 |
权重 |
评分标准 |
| 准确性 |
40% |
平台覆盖、数据解析、去重 |
| 完整性 |
30% |
字段完整、数量达标、分类覆盖 |
| 效率 |
30% |
响应时间、错误处理、资源占用 |
输出路径
- 数据输出:
workspace/data/output/trends.json
- 日志文件:
workspace/data/output/trends.log
依赖
# requirements.txt
requests>=2.28.0
beautifulsoup4>=4.11.0
lxml>=4.9.0
aiohttp>=3.8.0