Python爬取英雄聯(lián)盟MSI直播間彈幕并生成詞云圖
更新時間:2021年06月01日 09:53:48 作者:小菜雞也會有春天
很開心RNG最近在英雄聯(lián)盟季中賽奪冠了,特地爬取了直播間彈幕并生成詞云圖,大家一起開心一下,看看奪冠時大家都在說什么,需要的朋友可以參考下
一、環(huán)境準備
安裝相關第三方庫
pip install jieba
pip install wordcloud
二、數(shù)據(jù)準備
爬取對象:2021年5月23號,RNG奪冠直播間的彈幕信息
爬取對象路徑:
方式1、根據(jù)開發(fā)者工具(F12),獲取請求url、請求頭、cookie等信息;
方式2:根據(jù)直播地址url,前+字符i
我們這里演示的是,采用方式2。
三、代碼如下
import requests, re import jieba, wordcloud """ # 以下是練習代碼 def get_f12_barrage(url, headers, file_path): #通過f12中的,請求地址,獲取彈幕數(shù)據(jù) #:param url: 通過彈幕地址(通過開發(fā)者工具,從請求數(shù)據(jù)中獲取的) #:param headers: 請求頭信息 #:param file_path: 響應數(shù)據(jù)存儲路徑 #:return: 無返回數(shù)據(jù) # 1、發(fā)送請求,接受響應數(shù)據(jù) response = requests.get(url=url, headers=headers) # 2、獲取響應數(shù)據(jù) # 返回的響應可能是亂碼的,需要先轉成響應的編碼格式 # apparent_encoding的作用:會從網頁的內容中分析網頁編碼的方式 response.encoding = response.apparent_encoding # 3、解析響應數(shù)據(jù) content = response.text content_list = re.findall(":(.*?)@", content) # 4、存儲響應數(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): """ 通過請求網頁前面+i的彈幕地址,獲取彈幕數(shù)據(jù) :param url: 通過彈幕地址(網址前面+i) :param headers: 請求頭信息 :param file_path: 響應數(shù)據(jù)存儲路徑 :return: 無返回數(shù)據(jù) """ # 1、發(fā)送請求,接受響應數(shù)據(jù) response = requests.get(url=url, headers=headers) # 2、獲取響應數(shù)據(jù) # 返回的響應可能是亂碼的,需要先轉成響應的編碼格式 # apparent_encoding的作用:會從網頁的內容中分析網頁編碼的方式 response.encoding = response.apparent_encoding # 3、解析響應數(shù)據(jù) # 此時response.text可能是亂碼的 content = response.text # 正則匹配響應數(shù)據(jù),取數(shù)目標是(.*?)中的數(shù)據(jù) content_list = re.findall("<d p=\".*?\">(.*?)</d>", content) # 4、存儲響應數(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、打開文件,默認是gbk格式打開,這里指定utf-8打開 with open(word_file_path, encoding="utf-8") as fp: content = fp.read() # read方法返回的是一個字符串 # 2、利用結巴,分割詞,得到的是一個列表 content_list = jieba.lcut(content) # 再用空格,拼接字符串 content_str = " ".join(content_list) # 3、設置詞云圖 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ù)輸入文字內容,形成一個詞云圖,并存儲在指定路徑 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" """ # 請求路徑,是網頁前面+i url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=343174354' file_path = "e:\\RNG_1.txt" # 請求頭中需要包含cookie 和 user-agent,這些信息都可以用f12獲取到,注意有些字符需要轉義 # 為了個人信息安全,這里的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' } # 調用方法 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)
四、詞云圖效果展示
到此這篇關于Python爬取英雄聯(lián)盟MSI直播間彈幕并生成詞云圖的文章就介紹到這了,更多相關Python爬取彈幕并生成詞云圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python虛擬機字節(jié)碼教程之裝飾器實現(xiàn)詳解
在本篇文章當中主要給大家介紹在?cpython?當中一些比較常見的字節(jié)碼,從根本上理解?python?程序的執(zhí)行。在本文當中主要介紹一些?python?基本操作的字節(jié)碼,并且將從字節(jié)碼的角度分析函數(shù)裝飾器的原理2023-04-04Python中sorted()函數(shù)的強大排序技術實例探索
排序在編程中是一個基本且重要的操作,而Python的sorted()函數(shù)則為我們提供了強大的排序能力,在本篇文章中,我們將深入研究不同排序算法、sorted()?函數(shù)的靈活性,以及各種排序場景下的最佳實踐2024-01-01Python?NumPy實用函數(shù)筆記之allclose
這篇文章主要給大家介紹了關于Python?NumPy實用函數(shù)筆記之allclose的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-01-01