Python爬取英雄聯(lián)盟MSI直播間彈幕并生成詞云圖
一、環(huán)境準(zhǔn)備
安裝相關(guān)第三方庫
pip install jieba
pip install wordcloud
二、數(shù)據(jù)準(zhǔn)備
爬取對象:2021年5月23號,RNG奪冠直播間的彈幕信息
爬取對象路徑:
方式1、根據(jù)開發(fā)者工具(F12),獲取請求url、請求頭、cookie等信息;
方式2:根據(jù)直播地址url,前+字符i
我們這里演示的是,采用方式2。
三、代碼如下
import requests, re
import jieba, wordcloud
"""
# 以下是練習(xí)代碼
def get_f12_barrage(url, headers, file_path):
#通過f12中的,請求地址,獲取彈幕數(shù)據(jù)
#:param url: 通過彈幕地址(通過開發(fā)者工具,從請求數(shù)據(jù)中獲取的)
#:param headers:請求頭信息
#:param file_path: 響應(yīng)數(shù)據(jù)存儲路徑
#:return: 無返回數(shù)據(jù)
# 1、發(fā)送請求,接受響應(yīng)數(shù)據(jù)
response = requests.get(url=url, headers=headers)
# 2、獲取響應(yīng)數(shù)據(jù)
# 返回的響應(yīng)可能是亂碼的,需要先轉(zhuǎn)成響應(yīng)的編碼格式
# apparent_encoding的作用:會從網(wǎng)頁的內(nèi)容中分析網(wǎng)頁編碼的方式
response.encoding = response.apparent_encoding
# 3、解析響應(yīng)數(shù)據(jù)
content = response.text
content_list = re.findall(":(.*?)@", content)
# 4、存儲響應(yīng)數(shù)據(jù), ”w+格式“,即文件存在則,先清空再寫,不存在則新建文件
with open(file_path, mode="w+", encoding="utf-8") as fp:
for line in content_list:
# 分析每一行,發(fā)現(xiàn)第一個字符元素不是想要的,先去除第一個字符
fp.write(line[1:] + "\n")
return file_path
"""
# ******************8********************執(zhí)行代碼如下******************************************
# 算法:先爬取數(shù)據(jù),再根據(jù)爬取的數(shù)據(jù),生成詞云圖
def get_barrage(url, headers, file_path):
"""
通過請求網(wǎng)頁前面+i的彈幕地址,獲取彈幕數(shù)據(jù)
:param url: 通過彈幕地址(網(wǎng)址前面+i)
:param headers:請求頭信息
:param file_path: 響應(yīng)數(shù)據(jù)存儲路徑
:return: 無返回數(shù)據(jù)
"""
# 1、發(fā)送請求,接受響應(yīng)數(shù)據(jù)
response = requests.get(url=url, headers=headers)
# 2、獲取響應(yīng)數(shù)據(jù)
# 返回的響應(yīng)可能是亂碼的,需要先轉(zhuǎn)成響應(yīng)的編碼格式
# apparent_encoding的作用:會從網(wǎng)頁的內(nèi)容中分析網(wǎng)頁編碼的方式
response.encoding = response.apparent_encoding
# 3、解析響應(yīng)數(shù)據(jù)
# 此時response.text可能是亂碼的
content = response.text
# 正則匹配響應(yīng)數(shù)據(jù),取數(shù)目標(biāo)是(.*?)中的數(shù)據(jù)
content_list = re.findall("<d p=\".*?\">(.*?)</d>", content)
# 4、存儲響應(yīng)數(shù)據(jù), ”w+格式“,即文件存在則,先清空再寫,不存在則新建文件
with open(file_path, mode="w+", encoding="utf-8") as fp:
for line in content_list:
fp.write(line + "\n")
return file_path
# https://api.bilibili.com/x/v1/dm/list.so?oid=343174354
# https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=343174354&date=2021-05-23
def make_word_cloud(word_file_path):
# 1、打開文件,默認(rèn)是gbk格式打開,這里指定utf-8打開
with open(word_file_path, encoding="utf-8") as fp:
content = fp.read()# read方法返回的是一個字符串
# 2、利用結(jié)巴,分割詞,得到的是一個列表
content_list = jieba.lcut(content)
# 再用空格,拼接字符串
content_str = " ".join(content_list)
# 3、設(shè)置詞云圖
wc_config = wordcloud.WordCloud(
width=700,# 寬度
height=500, # 高度
background_color='black', # 背景色
font_path='msyh.ttc',# 找字體的配置路徑C:\Windows\Fonts\微軟雅黑,然后雙擊(出現(xiàn)3個),選擇【常規(guī)-屬性】
scale=15,# 顏色像素
stopwords={'了', '的'}, # 詞云圖中過濾掉一些字眼
contour_width=5, # 輪廓寬度
contour_color='red' # 輪廓顏色
)
# 4、根據(jù)輸入文字內(nèi)容,形成一個詞云圖,并存儲在指定路徑
wc_config.generate(content_str)
wc_config.to_file("e:\\reg_cloud.png")
return word_file_path
if __name__ == "__main__":
"""
# 從開發(fā)者工具或者抓包工具中獲取url地址
f12_url = 'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=343174354&date=2021-05-23'
# 從開發(fā)者工具或者抓包工具中,獲取請求頭信息,主要包括cookie、user-agent
header = {
'cookie': 'bsource=search_baidu; _uuid=BBD44BE2-5DAA-A13A-A593-FD3A83C3929423721infoc; buvid3=18335C4D-A3A0-48D5-973A-7943D63AC73E34756infoc; CURRENT_FNVAL=80; blackside_state=1; rpdid=|(um|u)klum~0J\'uYkkJJkJJu; fingerprint=be684c13c90e4279aaaa3e2694da4285; buvid_fp=18335C4D-A3A0-48D5-973A-7943D63AC73E34756infoc; buvid_fp_plain=96D5E0EF-41D9-4A32-BE47-DFF96C44DB9F155821infoc; SESSDATA=f90082f7%2C1637884658%2C008f9%2A51; bili_jct=2f17a04a5651fb1c6579b73aeb640f7f; DedeUserID=1153205015; DedeUserID__ckMd5=b6f334f0a2d86238; sid=6vb9j9nl; bfe_id=6f285c892d9d3c1f8f020adad8bed553',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
}
# 申明存儲文件的路徑
f12_file_path = "e:\\RNG.txt"
"""
# 請求路徑,是網(wǎng)頁前面+i
url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=343174354'
file_path = "e:\\RNG_1.txt"
# 請求頭中需要包含cookie 和 user-agent,這些信息都可以用f12獲取到,注意有些字符需要轉(zhuǎn)義
# 為了個人信息安全,這里的cookIe加密顯示
header = {
'cookie': '********',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
}
# 調(diào)用方法
get_barrage(url=url, headers=header, file_path=file_path)
word_file_path = get_barrage(url=url, headers=header, file_path=file_path)
make_word_cloud(word_file_path)
四、詞云圖效果展示

到此這篇關(guān)于Python爬取英雄聯(lián)盟MSI直播間彈幕并生成詞云圖的文章就介紹到這了,更多相關(guān)Python爬取彈幕并生成詞云圖內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信