酷代码 AI
菜单
服务商

Python代码实现:对B站天问一号事件评论进行情感分析与可视化

```python import requests from bs4 import BeautifulSoup import re import jieba.analyse import matplotlib.pyplot as plt from wordcloud import WordCloud import numpy as np import pandas as pd from snownlp import SnowNLP # 爬取Bilibili网站天问一号事件评论 def get_comments(url, headers): response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') comments = soup.find_all('div', class_='card') comment_list = [] for comment in comments: content = comment.find('span', class_='card-subtitle').get_text() comment_list.append(content) return comment_list # 情感分析 def sentiment_analysis(comment_list): sentiment_scores = [] for comment in comment_list: sentiment_scores.append(SnowNLP(comment).sentiments) return sentiment_scores # 绘制不同情感类型评论的数量分布饼状图 def draw_pie_chart(sentiment_scores): sentiment_counts = {"正面": 0, "中性": 0, "负面": 0} for score in sentiment_scores: if score > 0.6: sentiment_counts["正面"] += 1 elif score < 0.4: sentiment_counts["负面"] += 1 else: sentiment_counts["中性"] += 1 labels = list(sentiment_counts.keys()) sizes = [sentiment_counts[key] for key in labels] plt.figure(figsize=(8, 8)) plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90) plt.axis('equal') plt.title("不同情感类型评论的数量分布") plt.show() # 绘制每月评论量统计曲线图 def draw_line_chart(comments): dates = [comment['date'] for comment in comments] counts = [len(comments.loc[comments['date'] == date]) for date in dates] plt.figure(figsize=(10, 6)) plt.plot(dates, counts, marker='o') plt.xlabel("日期") plt.ylabel("评论量") plt.title("每月评论量统计图") plt.xticks(rotation=45) plt.grid(True) plt.show() # 绘制评论获赞数排名前十柱状图 def draw_bar_chart(comments): sorted_comments = comments.sort_values(by='likes', ascending=False).head(10) words = [comment['content'] for comment in sorted_comments] likes = [comment['likes'] for comment in sorted_comments] plt.figure(figsize=(10, 6)) plt.barh(range(len(words)), likes, color='skyblue') plt.yticks(range(len(words)), words) plt.xlabel("获赞数") plt.title("评论获赞数排名前十") plt.show() if __name__ == '__main__': url = "https://www.bilibili.com/video/BV1zZ4y1X7JQ" # 天问一号事件相关视频的URL headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"} comment_list = get_comments(url, headers) sentiment_scores = sentiment_analysis(comment_list) draw_pie_chart(sentiment_scores) draw_line_chart(pd.DataFrame(comment_list, columns=['content'])) draw_bar_chart(pd.DataFrame(comment_list, columns=['content'])) ``` 请注意替换`url`变量为实际的Bilibili网站天问一号事件相关视频的URL。 ######[AI写代码神器 | 1044点数解答 | 2024-06-20 10:30:59]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]