From ca9503cefc78081450ae29f301ae19986b1e52bf Mon Sep 17 00:00:00 2001 From: pbyilhaq2 <909434436@qq.com> Date: Wed, 18 Sep 2024 17:13:20 +0800 Subject: [PATCH] ADD file via upload --- 爬取单个视频弹幕.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 爬取单个视频弹幕.py diff --git a/爬取单个视频弹幕.py b/爬取单个视频弹幕.py new file mode 100644 index 0000000..800cfb5 --- /dev/null +++ b/爬取单个视频弹幕.py @@ -0,0 +1,16 @@ +import requests#爬取网页源码 +import re#对解析后的文件进行弹幕匹配 + +url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=1663877514' +headers = { + '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=url, headers=headers)#通过requests模块内get请求方式,对url发送请求 +response.encoding = response.apparent_encoding +print(response.text)#获取数据 +data_list = re.findall('(.*?)',response.text)#解析数据 +for index in data_list: + with open('弹幕.txt',mode='a',encoding='utf-8') as f:#输出为文本 + f.write(index) + f.write('\n')#换行 + print(index) \ No newline at end of file