|
|
@ -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('<d p=".*?">(.*?)</d>',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
|