|
|
@ -1,13 +1,54 @@
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
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' }
|
|
|
|
#从BV号获取CID
|
|
|
|
response = requests.get("https://api.bilibili.com/x/v1/dm/list.so?oid=1512399639", headers=headers) #要爬取的网址
|
|
|
|
def get_cid_from_bv(bv_ids):
|
|
|
|
response.encoding='utf-8' #编码方式
|
|
|
|
cids=[]
|
|
|
|
html = response.text
|
|
|
|
for bv_id in bv_ids:
|
|
|
|
soup = BeautifulSoup(html) #使用beautifulsoup库快速查找我们想要的信息
|
|
|
|
# 视频详情 API 地址
|
|
|
|
all_txt = soup.findAll("d") #寻找到所有包含d的行
|
|
|
|
video_url = f'https://api.bilibili.com/x/web-interface/view?bvid={bv_id}'
|
|
|
|
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] #将字符串中的空格消除掉
|
|
|
|
response = requests.get(video_url, headers=headers)
|
|
|
|
print(txtsss) ###打印便可看见一条条弹幕的属性和内容了。
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
data = response.json()
|
|
|
|
|
|
|
|
# 提取 cid
|
|
|
|
|
|
|
|
if data.get('code') == 0:
|
|
|
|
|
|
|
|
cid = data.get('data', {}).get('cid')
|
|
|
|
|
|
|
|
cids.append(cid)
|
|
|
|
|
|
|
|
return cids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_danmu(id):
|
|
|
|
|
|
|
|
global videosnumber
|
|
|
|
|
|
|
|
video_url = f'https://api.bilibili.com/x/v1/dm/list.so?oid={id}'
|
|
|
|
|
|
|
|
response = requests.get(video_url, 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] #将字符串中的空格消除掉
|
|
|
|
|
|
|
|
videosnumber = videosnumber +1
|
|
|
|
|
|
|
|
bulletnumber = len(txtsss)
|
|
|
|
|
|
|
|
print( f"这是第{videosnumber}视频, 获取到{bulletnumber}弹幕")
|
|
|
|
|
|
|
|
time.sleep(1)#休眠防被封
|
|
|
|
|
|
|
|
return(txtsss)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(kword,mubiao):
|
|
|
|
|
|
|
|
bvs=[]
|
|
|
|
|
|
|
|
alltxt=[]
|
|
|
|
|
|
|
|
for i in range(100):
|
|
|
|
|
|
|
|
cids = []
|
|
|
|
|
|
|
|
cids = get_cid_from_bv(bvs)
|
|
|
|
|
|
|
|
for id in cids:
|
|
|
|
|
|
|
|
if(videosnumber>=mubiao): break
|
|
|
|
|
|
|
|
txt = get_danmu(id)
|
|
|
|
|
|
|
|
alltxt=alltxt + txt
|
|
|
|
|
|
|
|
if(videosnumber>=mubiao): break
|
|
|
|
|
|
|
|
return(alltxt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
flag = 50 #你要爬的视频数量
|
|
|
|
|
|
|
|
altxt=main(keword,flag)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|