|
|
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") |