From bc4d936bb7f5c03fc57441b7abc3accf83a3f7b6 Mon Sep 17 00:00:00 2001 From: pbyilhaq2 <909434436@qq.com> Date: Wed, 18 Sep 2024 19:04:11 +0800 Subject: [PATCH] ADD file via upload --- 获取视频的cid,并进行提取.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 获取视频的cid,并进行提取.py diff --git a/获取视频的cid,并进行提取.py b/获取视频的cid,并进行提取.py new file mode 100644 index 0000000..430fd04 --- /dev/null +++ b/获取视频的cid,并进行提取.py @@ -0,0 +1,20 @@ +import requests +import json +import re +#使用get_barrage()函数来获取视频弹幕 +def get_barrage(bvid,headers): + url = "https://api.bilibili.com/x/web-interface/view?bvid=" + bvid #B站API获取CID + req = requests.get(url, headers=headers) + res = json.loads(req.text) + cid = res['data']['cid']#提取弹幕cid。 + + barrage_url = "https://api.bilibili.com/x/v2/dm/web/seg.so?type=1&oid=" + str(cid) + "&segment_index=1"#获取弹幕XML文件 + barrage_req = requests.get(barrage_url, headers=headers) #发送GET请求 + barrage_req.encoding = 'utf-8' + barrage_list = re.findall('(.*?)',barrage_req.text) #解析数据 + for index in barrage_list: + with open('barrage.txt', mode='a', encoding='utf-8') as f:#输出为文本 + f.write(index) + f.write("\n") + print(index) + return barrage_list \ No newline at end of file