From ef2311f30cce44b9472b8267b6f8fd94591b8e04 Mon Sep 17 00:00:00 2001 From: pmgp6jfbh <1072906427@qq.com> Date: Thu, 13 Nov 2025 15:45:07 +0800 Subject: [PATCH] ADD file via upload --- main.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..531698d --- /dev/null +++ b/main.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +import os # 新增:导入os模块 +from crawler import BilibiliDanmakuCrawler +from statistic import DanmakuStatistic +from visualization import DanmakuVisualizer +from analysis import DanmakuAnalyzer # 新增:添加分析模块 + +def main(): + # 配置参数 + keyword = "大语言模型" + video_count = 300 + data_dir = "./data" + + # 新增:确保数据目录存在(关键修复) + try: + os.makedirs(data_dir, exist_ok=True) + print(f"数据目录已准备就绪:{data_dir}") + except PermissionError: + print(f"错误:没有权限创建目录 {data_dir},请检查路径权限") + return + except Exception as e: + print(f"创建目录失败:{e}") + return + + # 1. 爬取弹幕 + print(f"===== 开始爬取「{keyword}」相关视频弹幕 =====") + crawler = BilibiliDanmakuCrawler( + keyword=keyword, + video_count=video_count, + save_dir=data_dir + ) + crawler.run() + + # 检查弹幕文件是否存在 + danmu_file = f"{data_dir}/all_danmu.txt" + if not os.path.exists(danmu_file) or os.path.getsize(danmu_file) == 0: + print("错误:弹幕文件不存在或为空,无法继续统计") + return + + # 2. 统计高频词 + print("\n===== 开始统计弹幕高频词 =====") + stat_file = f"{data_dir}/statistic.xlsx" + try: + stat = DanmakuStatistic( + danmu_path=danmu_file, + excel_path=stat_file + ) + stat.export_to_excel() + # 检查统计文件是否生成成功 + if not os.path.exists(stat_file) or os.path.getsize(stat_file) == 0: + print("警告:统计文件生成失败,将跳过可视化步骤") + return + except PermissionError: + print(f"错误:没有权限写入统计文件 {stat_file},请关闭可能占用该文件的程序") + return + except Exception as e: + print(f"统计过程出错:{e}") + return + + # 3. 生成可视化结果 + print("\n===== 开始生成可视化结果 =====") + try: + viz = DanmakuVisualizer( + danmu_path=danmu_file, + excel_path=stat_file + ) + viz.generate_wordcloud() + viz.plot_top8_bar() + except Exception as e: + print(f"可视化过程出错:{e}") + + # 4. 新增:观点分析 + print("\n===== 开始进行用户观点分析 =====") + analyzer = DanmakuAnalyzer(danmu_path=danmu_file) + analyzer.analyze() + +if __name__ == "__main__": + main() \ No newline at end of file