from wordcloud import WordCloud from collections import Counter import matplotlib.pyplot as plt import pandas as pd final_danmu=[]#所有弹幕合集 final_danmu_ai=[]#所有ai有关弹幕合计 for num in range(1,301):#遍历爬好的弹幕 text_name=f"第{num}个视频弹幕.txt" danmu=[] key=[ '人工智能','AI' ] try: with open(f"{text_name}",mode='r',encoding='utf-8') as f: for i in f: danmu.append(i.strip()) danmu_ai=[i for i in danmu if any(j in i for j in key)] final_danmu_ai+=danmu_ai final_danmu+=danmu except: continue aicount=Counter(final_danmu_ai) danmucount=Counter(final_danmu) #分别计数 for i in aicount.most_common(8):#AI有关弹幕排名前8 print(f"弹幕:{i[0]}\n次数:{i[1]}\n") wordcloud = WordCloud(width=800, height=400, background_color='white', font_path='msyh.ttc', # 设置字体路径,可以根据系统字体路径调整 max_words=100, colormap='viridis').generate_from_frequencies(danmucount) # 显示词云图 plt.figure(figsize=(10, 5)) plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') # 不显示坐标轴 plt.show() #写入excel df = pd.DataFrame(danmucount.items(), columns=['弹幕内容', '出现次数']) df.to_excel("弹幕大全.xlsx", index=False)