import pandas as pd
import re
import requests
from bs4 import BeautifulSoup
time_nature=[]
comments=[]
url = input('请输入B站视频链接: ')
res = requests.get(url)
cid = re.findall(r'"cid":(.*?),', res.text)[0]
url = f'https://comment.bilibili.com/{cid}.xml'
print(url)
request = requests.get(url)
request.encoding='utf8'
soup = BeautifulSoup(request.text, 'lxml')
results = soup.find_all('d')
for t in soup.find_all('d'):
time_nature.append(t.attrs['p'])
comments.append(t.text)
print(t.attrs['p'])
print(t.text)
df = pd.DataFrame()
df['时间属性'] = time_nature
df['弹幕内容'] = comments
df.to_excel('b站弹幕.xls')