1、b站网址:https://www.bilibili.com/bangumi/play/ss11936/
2、弹幕网址:http://comment.bilibili.com/13306224.xml,13306224为cid
3、用python 编写爬虫,并进行文本分析,生成词云。
import urllib.request
import urllib.error
import pandas as pd
import time
import re
import sys
from lxml import etree
import requests
from requests import get
import jieba
import numpy as np #numpy计算包
from wordcloud import WordCloud#词云包
import matplotlib.pylab as pyl#折线图、散点图plot,直方图hist
import matplotlib.pyplot as plt
headers=("user-agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
opener=urllib.request.build_opener()
opener.addheaders=[headers]
urllib.request.install_opener(opener)#添加opener为全局
comments=""
try:
url="http://comment.bilibili.com/13306224.xml"
#解码url
data = requests.get(url).content
html = etree.HTML(data)
comment_list = html.xpath('//d/text()')
n=len(comment_list)
# comment_list[index]返回的是一个字典
for j in range(0,n):
non_num = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
comment_list[j]=comment_list[j].translate(non_num)
comments = comments + (str(comment_list[j])).strip()
pattern = re.compile(r'[\u4e00-\u9fa5]+')#清洗标点符号,至少匹配一个汉字的写法
filterdata = re.findall(pattern, comments)
cleaned_comments = ''.join(filterdata)#将序列中的元素以指定的字符连接生成一个新的字符串。
segment = jieba.lcut(cleaned_comments)#返回list
words_df=pd.DataFrame({'segment':segment})#segment列名
stopwords=pd.read_csv("chineseStopWords.txt",index_col=False,quoting=3,sep="\t",names=['stopword'], encoding='utf-8')#quoting=3全不引用,index_col为默认为None,即不指定行索引,系统自动加上行索引(0-),
words_df=words_df[~words_df.segment.isin(stopwords.stopword)]#去掉在stopword中显示的文字,words_df的type仍然是DataFrame
words_stat = words_df.groupby(by=['segment'])['segment'].agg(np.size) #以segment分类,以计数对该列聚合,size()函数主要是用来统计矩阵元素个数
words_stat = words_stat.to_frame()
words_stat.columns = ['计数']
words_stat = words_stat.reset_index().sort_values(by=["计数"], ascending=False)
print(words_stat)
wordcloud=WordCloud(font_path="Lib/site-packages/wordcloud/font163/simheittf.ttf",background_color="white", max_words=100,max_font_size=200,width=1000,height=600,random_state=50) #指定字体类型、字体大小和字体颜色,设置有多少种随机生成状态,即有多少种配色方案
word_frequence = {x[0]:x[1] for x in words_stat.head(1000).values}
wordcloud=wordcloud.fit_words(word_frequence)
wordcloud.to_file('cloudword.jpg')
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
time.sleep(10)
except Exception as e:
print("exception:"+str(e))#若为Exception异常,延时10秒执行
time.sleep(10)
4、词频分析,前四个联合起来“择一事终一生”,是电影的主题。这就是所谓的“匠心”,一代又一代修复师们用自己的事迹做了完美诠释。然后看到两个亮点,一个是御猫,一个是修复师们(尤其是王师傅,不愧是故宫男神)。
segment 计数
9 一事 625
82 一生 622
5359 终 586
3360 择 508
3551 故宫 469
2158 好 364
5659 致敬 193
1684 哈哈哈 183
3603 文物 179
4599 猫 145
2826 弹幕 137
4979 真的 137
2652 师傅 137
3034 想 135
1239 北京 132
363 中国 131
4633 王师傅 130
4177 没 119
1748 喜欢 119
3074 感谢 111
2294 学 106
5854 表白 104
4776 男神 94
1356 厉害 90
772 修 87
6048 谢谢 81
1505 可爱 81
5314 纪录片 76
305 专业 74
779 修复 73
... ... ...
2813 弟子 1
2812 弘扬 1
2811 引线 1
2810 引物 1
2809 引来 1
2808 引人瞩目 1
2807 式 1
2806 弄死 1
2805 弄不好 1
2827 弹破琴 1
2828 弹簧片 1
2831 强制 1
2844 当做 1
2856 录下来 1
2854 当面 1
2853 当缴 1
2851 当狗 1
2850 当心 1
2849 当御 1
2846 当只 1
2843 当个 1
2832 强势 1
2842 当不起 1
2841 当下 1
2840 彈幕 1
2839 强项 1
2837 强行 1
2835 强推 1
2834 强悍 1
6973 龟 1
5、词云
最后想说,我决定明年去首都逛故宫。