爬取B站弹幕并生成HTML格式的词云图
发布时间
阅读量:
阅读量
目录
一、导入模块部分
二、输入cid部分
三、爬取弹幕内容、设置列表、数据字典及词云图的创建
四、输出结果示例
(一)RUN窗格对话:
(二)Pycharm相应内容
(三)按照保存地址保存好的HTML文件
(四)双击文件,使用浏览器打开的内容
五、全部代码
一、导入模块部分
首先是需要5个模块:
1⃣️requesets
2⃣️bs4
3⃣️jieba
4⃣️pyecharts 1.8.1
5⃣️lxml(虽然没有使用import,但是这个要装,在代码中会用到)
#引入requests请求模块
import requests
#引入bs4模块
from bs4 import BeautifulSoup
#引入结巴库
import jieba
#引入词云模块
from pyecharts.charts import WordCloud
二、输入cid部分
#手动输入需要爬取的弹幕cid
cid = str(input("请输入要爬取视频的cid:"))
#设置弹幕api
url = f"https://comment.bilibili.com/{cid}.xml"
#获取链接内容赋值给response
response = requests.get(url)
#确定网页代码
response.encoding = response.apparent_encoding
#通过.text返回网页xml文本内容
xml = response.text
#使用lxml方法分析xml中的节点赋值给soup
soup = BeautifulSoup(xml,"lxml")
#使用find_all方法查找在soup中为"d"的节点
content_all = soup.find_all("d")
其中cid是弹幕评论API的一部分,查找方式如下图:

将选中内容的cid代码部分复制下来,运行的时候粘贴就行。
三、爬取弹幕内容、设置列表、数据字典及词云图的创建
⚠️要注意更改词云图保存的地址⚠️
#为弹幕数量设置一个初始值
comment_num = 0
#创建一个空白列表给词云
wordList = []
#遍历content_all中的所有节点
for comment in content_all:
#将每次遍历到的节点的内容,转换为字符串赋值给data
data = comment.string
#视小于2且大于10的评论为非弹幕
if len(data) >= 2 and len(data) <=10:
# 使用jieba.lcut()将data进行分词,赋值给words
words = jieba.lcut(data)
#将切好的词放入列表当中
wordList = wordList + words
#每次成功判断,计算一次有效弹幕数
comment_num += 1
#输入当前遍历的弹幕内容
print(data)
#否则
else:
#跳过并继续
continue
#设置一个空字典
wordDict = {}
#从列表中遍历已经分好的词
for word in wordList:
#如果词的长度大于1,怎判断为一个词
if len(word) > 1:
#如果这个词没有在字典的键中
if word not in wordDict.keys():
#设置这个键的初始值为1
wordDict[word] = 1
#否则
else:
#这个词的值+1
wordDict[word] += 1
#把WordCloud赋值给wordCloud
wordCloud = WordCloud()
#添加图标设置。data——pair会警告,但无影响
wordCloud.add( series_name=f"{comment_num}条弹幕的词云图",
data_pair = wordDict.items(),
word_size_range=[20,80])
#设置保存的地址
wordCloud.render(f"/Users/huangliangyu/Desktop/{comment_num}条弹幕的词云图.html")
#格式化输出统计数字
print(F"该视频共有{comment_num}条有效弹幕")
四、输出结果示例
(一)RUN窗格对话:

(二)Pycharm相应内容

(三)按照保存地址保存好的HTML文件

(四)双击文件,使用浏览器打开的内容

五、全部代码
#引入requests请求模块
import requests
#引入bs4模块
from bs4 import BeautifulSoup
#引入结巴库
import jieba
#引入词云模块
from pyecharts.charts import WordCloud
#手动输入需要爬取的弹幕cid
cid = str(input("请输入要爬取视频的cid:"))
#设置弹幕api
url = f"https://comment.bilibili.com/{cid}.xml"
#获取链接内容赋值给response
response = requests.get(url)
#确定网页代码
response.encoding = response.apparent_encoding
#通过.text返回网页xml文本内容
xml = response.text
#使用lxml方法分析xml中的节点赋值给soup
soup = BeautifulSoup(xml,"lxml")
#使用find_all方法查找在soup中为"d"的节点
content_all = soup.find_all("d")
#为弹幕数量设置一个初始值
comment_num = 0
#创建一个空白列表给词云
wordList = []
#遍历content_all中的所有节点
for comment in content_all:
#将每次遍历到的节点的内容,转换为字符串赋值给data
data = comment.string
#视小于2且大于10的评论为非弹幕
if len(data) >= 2 and len(data) <=10:
# 使用jieba.lcut()将data进行分词,赋值给words
words = jieba.lcut(data)
#将切好的词放入列表当中
wordList = wordList + words
#每次成功判断,计算一次有效弹幕数
comment_num += 1
#输入当前遍历的弹幕内容
print(data)
#否则
else:
#跳过并继续
continue
#设置一个空字典
wordDict = {}
#从列表中遍历已经分好的词
for word in wordList:
#如果词的长度大于1,怎判断为一个词
if len(word) > 1:
#如果这个词没有在字典的键中
if word not in wordDict.keys():
#设置这个键的初始值为1
wordDict[word] = 1
#否则
else:
#这个词的值+1
wordDict[word] += 1
#把WordCloud赋值给wordCloud
wordCloud = WordCloud()
#添加图标设置。data——pair会警告,但无影响
wordCloud.add( series_name=f"{comment_num}条弹幕的词云图",
data_pair = wordDict.items(),
word_size_range=[20,80])
#设置保存的地址
wordCloud.render(f"/Users/huangliangyu/Desktop/{comment_num}条弹幕的词云图.html")
#格式化输出统计数字
print(F"该视频共有{comment_num}条有效弹幕")
全部评论 (0)
还没有任何评论哟~
