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