From 3d895f34ed9dd4abd90b75f93551cccb2b358a8e Mon Sep 17 00:00:00 2001 From: pw4izm9rs <2764851313@qq.com> Date: Wed, 18 Sep 2024 20:27:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=84=E5=8A=A0=E9=A2=982=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- get_danmu_chart.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 get_danmu_chart.py diff --git a/get_danmu_chart.py b/get_danmu_chart.py new file mode 100644 index 0000000..cc1b773 --- /dev/null +++ b/get_danmu_chart.py @@ -0,0 +1,54 @@ +import requests +from bs4 import BeautifulSoup +import matplotlib.pyplot as plt + +url = "https://api.bilibili.com/x/v1/dm/list.so?oid=1643253304" +# 伪装成浏览器访问 +header = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0"} + +response = requests.get(url, headers=header) +response.encoding = 'utf-8' +xml = response.text +soup = BeautifulSoup(xml, "lxml") +content_all = soup.find_all(name="d") + +timeList = [] + +# for循环遍历content_all +for content in content_all: + # 使用.attrs获取p对应的属性值,并赋值给data + data = content.attrs["p"] + + # 使用split()函数分割data,把第一个元素时间赋值给time + time = data.split(",")[0] + + # 将time转换成浮点数,添加进列表timeList中 + timeList.append(float(time)) + +danmu_dict = {} + +for x in range(7): + start = 30 * x + 1 + end = 30 * (x + 1) + segment_range = f"{start}-{end}" + danmu_dict[segment_range] = 0 + +for danmu in danmu_dict.keys(): + start_key = danmu.split("-")[0] + end_key = danmu.split("-")[1] + for time in timeList: + if int(start_key) <= time <= int(end_key): + danmu_dict[danmu] = danmu_dict[danmu] + 1 + +path = 'D:/雷电大魔王/Python/软工作业/软工作业/' # 保存路径 +name = 'danmu' # 图表名称 +plt.title(name+"_chart") +plt.rc('font',family='SimHei'); plt.rc('font',size=17) +#提取字典的键和值 +x = list(danmu_dict .keys()) +y = list(danmu_dict.values()) +#绘制折线图 +plt.plot(x, y) +plt.xlabel('X') +plt.ylabel('Y') +plt.savefig(path + "/" + r"弹幕统计图.png") \ No newline at end of file