1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Python哔哩哔哩弹幕爬取+词云生成

Python哔哩哔哩弹幕爬取+词云生成

时间:2018-11-28 14:32:36

相关推荐

Python哔哩哔哩弹幕爬取+词云生成

心血来潮,研究下词云,先上图,最终效果

代码

import numpy as npimport requestsfrom bs4 import BeautifulSoupfrom wordcloud import WordCloudimport PIL.Image as imageimport jieba# 爬取弹幕url= '/128614096.xml'request = requests.get(url)#获取页面request.encoding='utf8'#因为是中文,我们需要进行转码,否则出来的都是unicodesoup = BeautifulSoup(request.text, 'lxml')results = soup.find_all('d')#找出所有'd'comments = [comment.text for comment in results]#因为出来的时候是bs4格式的,我们需要把他转化成liscomments = [x.upper() for x in comments]#统一大小写comments_clean = [comment.replace(' ','') for comment in comments]#去掉空格set(comments_clean)#看一下都有啥类似的没用的词语useless_words = ['//TEST','/TESR','/TEST','/TEST/','/TEXT','/TEXTSUPREME','/TSET','/Y','\\TEST']comments_clean = [element for element in comments_clean if element not in useless_words]#去掉不想要的字符fnl_words = [word for word in comments_clean if len(word)>1]#去掉单字st = ''.join(fnl_words)#拼成一个字符串cut_text = " ".join(jieba.cut(st)) #分词mask = np.array(image.open(r"C:\Users\jh\PycharmProjects\ftp\mask.png"))wordcloud = WordCloud(# 添加遮罩层mask=mask,# 生成中文字的字体,必须要加,不然看不到中文font_path = "C:\Windows\Fonts\msyh.ttc").generate(cut_text)image_produce = wordcloud.to_image()image_produce.show()

注意:

####### 想制作别的视频词云只需要更换URL即可 , 哔哩哔哩cid自行百度

需要用到的mask图片

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。