From fbdc723e937dd0205a6e02204fbaf90f619dee2b Mon Sep 17 00:00:00 2001 From: QMZ <1164250597@qq.com> Date: Fri, 13 Sep 2024 22:19:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=8D=95=E4=B8=AA=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E7=9A=84=E5=BC=B9=E5=B9=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- getdanmu.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 getdanmu.py diff --git a/getdanmu.py b/getdanmu.py new file mode 100644 index 0000000..a0d9a03 --- /dev/null +++ b/getdanmu.py @@ -0,0 +1,13 @@ +import requests +from bs4 import BeautifulSoup +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("https://api.bilibili.com/x/v1/dm/list.so?oid=1512399639", headers=headers) #要爬取的网址 +response.encoding='utf-8' #编码方式 +html = response.text +soup = BeautifulSoup(html) #使用beautifulsoup库快速查找我们想要的信息 +all_txt = soup.findAll("d") #寻找到所有包含d的行 +txt=[all_txts.attrs ["p"]for all_txts in all_txt] #寻找到所有包含d的行中属性为p的值,这里边包含了弹幕的虚拟id等 +txtss=[all_txts.string for all_txts in all_txt] #寻找到所有包含d的行中的字符串数据,即弹幕内容 +txtsss=[txts.replace(' ','') for txts in txtss] #将字符串中的空格消除掉 +print(txtsss) ###打印便可看见一条条弹幕的属性和内容了。 +