Files
BDMA-exp-2/README.md
2026-03-26 18:49:14 +08:00

136 lines
3.9 KiB
Markdown
Raw Permalink 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. 项目说明
本项目按实验流程完成以下任务:
- 评论清洗、去重、分词与停用词过滤
- SnowNLP情感标注与可视化分析
- 两种TF-IDF方案+朴素贝叶斯建模对比
- 加入时间与点赞特征后再次训练评估
- src/byd_sentiment_pipeline.py: 主流程脚本
- cn_stopwords.txt: 停用词表
- .env.example: 环境变量模板
- data/: 中间数据与结果CSV
- outputs/: 图像与分析报告
## 2. 环境准备
1. 创建并激活虚拟环境
2. 安装依赖
```bash
pip install -r requirements.txt
```
## 3. Cookie配置
复制模板生成.env文件
```bash
cp .env.example .env
```
在.env中填写真实Cookie。Cookie不会写进代码。
推荐按下面步骤获取,成功率更高:
1. 浏览器登录B站账号打开任意一个目标视频页面并停留10秒。
2. 按F12打开开发者工具切到Network。
3. 刷新页面后在请求列表中选中任意一个发往api.bilibili.com的请求常见如x/v2/reply、x/web-interface/view
4. 在Headers里复制两项
- Request Headers -> Cookie 整段值粘贴到BILI_COOKIE。
- Request Headers -> User-Agent粘贴到BILI_USER_AGENT。
5. 确保Cookie是一整行不要换行不要多余引号。
示例(仅演示格式):
```env
BILI_COOKIE=SESSDATA=xxxx; bili_jct=xxxx; DedeUserID=xxxx; DedeUserID__ckMd5=xxxx; buvid3=xxxx; buvid4=xxxx;
BILI_USER_AGENT=Mozilla/5.0 (...) Chrome/124.0.0.0 Safari/537.36
```
先做自检再爬取:
```bash
python - <<'PY'
import os
from dotenv import load_dotenv
load_dotenv('.env')
cookie = os.getenv('BILI_COOKIE', '')
ua = os.getenv('BILI_USER_AGENT', '')
print('Cookie已配置:', bool(cookie), '长度:', len(cookie))
print('UA已配置:', bool(ua), '长度:', len(ua))
PY
```
如果出现412或-352
1. 重新复制一次最新Cookie过期很常见
2. 降低抓取频率增大sleep参数
3. 避免同一时间多终端并发跑爬虫。
## 5. 运行方式
全流程执行:
```bash
python src/byd_sentiment_pipeline.py all --target-comments 4500 --min-comments 4000
```
分阶段执行:
```bash
python src/byd_sentiment_pipeline.py crawl --target-comments 4500 --min-comments 4000
python src/byd_sentiment_pipeline.py preprocess
python src/byd_sentiment_pipeline.py explore
python src/byd_sentiment_pipeline.py model
```
说明:
1. crawl阶段默认支持断点续抓。若data/byd_comments_raw.csv已存在会自动读取并按评论ID/评论内容+时间+BV号去重后继续抓取。
2. 因为B站接口可能间歇返回412建议多轮执行crawl累积到目标条数不会重复叠加历史评论。
爬虫建议使用更慢参数减少412概率
```bash
python src/byd_sentiment_pipeline.py crawl --target-comments 4500 --min-comments 4000 --sleep-min 1.2 --sleep-max 2.5
```
如果想自动多轮累积每轮都会基于历史CSV去重后续抓可使用
```bash
python src/byd_sentiment_pipeline.py crawl --target-comments 4500 --min-comments 4000 --sleep-min 1.2 --sleep-max 2.5 --rounds 6 --round-cooldown 90
```
## 5.1 B站场景停用词补充
如果词云或top_words中出现“回复、展开、置顶”等平台噪声词可直接在cn_stopwords.txt中每行追加一个词然后重新执行
```bash
python src/byd_sentiment_pipeline.py preprocess
python src/byd_sentiment_pipeline.py explore
```
## 6. 输出文件
- data/byd_comments_raw.csv
- data/byd_comments_preprocessed.csv
- data/byd_comments_labeled.csv
- outputs/overall_wordcloud.png
- outputs/负面\_wordcloud.png
- outputs/中性\_wordcloud.png
- outputs/正面\_wordcloud.png
- outputs/sentiment_pie.png
- outputs/monthly_trend.png
- outputs/top10_likes_bar.png
- outputs/confusion_method1_default.png
- outputs/confusion_method2_improved.png
- outputs/confusion_method2_plus_features.png
- outputs/model_metrics_summary.csv
- outputs/exploration_report.md
- outputs/model_report.md