我們使用beatifulsop爬取到B站視頻的字幕:https://www.cnblogs.com/becks/p/14540355.html
然後將爬取的字幕,使用pandas處理後寫到CSV文件中:https://www.cnblogs.com/becks/p/14738496.html
本篇,實現將字幕分詞處理後詞雲化顯示,可更加直觀的瞭解字幕的核心詞彙都有哪些
思路,爬到字幕後,將字幕分詞處理,統計詞頻(所有詞的出現頻率),然後根據出現頻率設置詞的顯示大小
需要用到以下庫
from bs4 import BeautifulSoup#爬蟲 import requests import pandas as pd#數據處理 import jieba#分詞 from collections import Counter#分詞後詞頻統計 from pyecharts.charts import WordCloud#詞雲
如果出現下圖中no module named‘pandas’,提示,即缺少上面需要的庫,可通過cmd執行pip install xxx(這裏是pandas)來安裝
安裝pyecharts時需要注意,本案例使用的是1.9.0版本,0.x.x版本與1.9.0版本差別很大互不兼容,可能會出現很多錯誤或者方法不可用,請注意版本
爬取和數據提取在前面兩篇文章已經介紹過了,直接貼代碼部分
#獲取彈幕 url = 'http://comment.bilibili.com/309778762.xml' html = requests.get(url) html.encoding='utf8' soup = BeautifulSoup(html.text,'lxml') results = soup.find_all('d') #對彈幕數據進行處理,主要是提取彈幕內容,去掉標籤和其他html符號 comments = [comment.text for comment in results]#從爬取的數據中取出彈幕數據,返回文本內容
分詞和統計詞頻,中間的步驟如果需要了解的話,建議每一步後面print一下定義的變量,這樣纔可以更加直觀的看到數據在所有步驟中是如何轉換的
#分詞及統計詞頻 text = ''.join(comments)#對獲取的comments數據進行處理,去除中間的‘,’,具體參考https://blog.csdn.net/weixin_42986099/article/details/83447926 words = list(jieba.cut(text))#jieba分詞 ex_sw_words = [] for word in words: if len(word)>1:#去掉字數不足2個的詞 ex_sw_words.append(word) c = Counter() c = Counter(ex_sw_words)#統計分詞後所有詞語出現的頻率 wc_data = pd.DataFrame({'word':list(c.keys()), 'counts':list(c.values())}).sort_values(by='counts', ascending=False).head(100)
#重建數據,具體參考https://www.cnblogs.com/andrew-address/p/13040035.html
a = wc_data['word'].values.tolist() b = wc_data['counts'].values.tolist() name_tulpe = list(zip(a,b))#創建集合,使數據滿足wordcloud對傳入數據的要求
生成詞雲,wordcloud 對傳入參數有嚴格的要求,具體參考文內的鏈接
#wordcloud對傳入參數的要求參考 # https://gallery.pyecharts.org/#/WordCloud/wordcloud_custom_mask_image # https://pyecharts.org/#/zh-cn/basic_charts?id=wordcloud:詞雲圖 #將分詞和統計詞頻後的內容用詞雲顯示出來 wordcloud = WordCloud() wordcloud.add("",name_tulpe,word_size_range=[15, 80]) wordcloud.render('wordcloud.html')
執行腳本後生成的詞雲效果
附全部腳本
# -*- coding: utf-8 -*- from bs4 import BeautifulSoup#爬蟲 import requests import pandas as pd#數據處理 import jieba#分詞 from collections import Counter#分詞後詞頻統計 from pyecharts.charts import WordCloud#詞雲 #獲取彈幕 url = 'http://comment.bilibili.com/309778762.xml' html = requests.get(url) html.encoding='utf8' soup = BeautifulSoup(html.text,'lxml') results = soup.find_all('d') #對彈幕數據進行處理,主要是提取彈幕內容,去掉標籤和其他html符號 comments = [comment.text for comment in results]#從爬取的數據中取出彈幕數據,返回文本內容 #分詞及統計詞頻 text = ''.join(comments)#對獲取的comments數據進行處理,去除中間的‘,’,具體參考https://blog.csdn.net/weixin_42986099/article/details/83447926 words = list(jieba.cut(text))#jieba分詞 ex_sw_words = [] for word in words: if len(word)>1:#去掉字數不足2個的詞 ex_sw_words.append(word) c = Counter() c = Counter(ex_sw_words)#統計分詞後所有詞語出現的頻率 wc_data = pd.DataFrame({'word':list(c.keys()), 'counts':list(c.values())}).sort_values(by='counts', ascending=False).head(100)#重建數據,具體參考https://www.cnblogs.com/andrew-address/p/13040035.html a = wc_data['word'].values.tolist() b = wc_data['counts'].values.tolist() name_tulpe = list(zip(a,b))#創建集合,使數據滿足wordcloud對傳入數據的要求 #wordcloud對傳入參數的要求參考 # https://gallery.pyecharts.org/#/WordCloud/wordcloud_custom_mask_image # https://pyecharts.org/#/zh-cn/basic_charts?id=wordcloud:詞雲圖 #將分詞和統計詞頻後的內容用詞雲顯示出來 wordcloud = WordCloud() wordcloud.add("",name_tulpe,word_size_range=[15, 80]) wordcloud.render('wordcloud.html')