一、首先找到含有音频和视频的url地址
打开一个视频,刷新后,找到这个包,里面有我们所需要的数据
访问这个数据包后,获取字符串数据,用正则提取,再转为json字符串方便提取。
二、获得标题和音频数据后,分别获取二进制数据,进行保存,注意这里访问时,必须加上防盗链即refer。
完整代码:
python
import requests
import json
import re
import pprint
def get_response(url):
headers ={
"Referer":"https://space.bilibili.com/480670664",
"Cookie":"buvid3=147D8190-C7BD-7BA9-1991-11F625BFAD0F63250infoc; b_nut=1704603163; i-wanna-go-back=-1; b_ut=7; _uuid=24B48ADE-5F45-F1BA-8F101-92E6EA3C5E9562728infoc; enable_web_push=DISABLE; buvid4=54DF23A6-E7BA-5931-0E4C-64707AFE8C9F64640-024010704-8qrN5r0k2p518FoBUY6DBs00hx1F24CGrUVZ2ENL3N3IDPtdKOB3sdEUTsoRlc77; LIVE_BUVID=AUTO7417046031702675; buvid_fp_plain=undefined; header_theme_version=CLOSE; is-2022-channel=1; CURRENT_BLACKGAP=0; rpdid=0zbfVHa4e0|Cpjx3t0|fIj8|3w1RshGT; DedeUserID=151028355; DedeUserID__ckMd5=c9e195efef0967b0; fingerprint=a0cfdd0d76b3941516e3c9cfbdb4cde2; buvid_fp=a0cfdd0d76b3941516e3c9cfbdb4cde2; CURRENT_FNVAL=4048; CURRENT_QUALITY=80; hit-dyn-v2=1; home_feed_column=4; bp_video_offset_151028355=910186544512368662; b_lsid=DF21B14C_18E89FDCDDC; bmg_af_switch=1; bmg_src_def_domain=i0.hdslb.com; FEED_LIVE_VERSION=V_HEADER_LIVE_NO_POP; browser_resolution=1382-742; SESSDATA=3453694a%2C1727264098%2C76879%2A32CjATHAdI0T0tgl7P0OCtAji8X9-btkgYMjqQGtt0B5NBVhXBvhYdTL9LfXB0kiUzKLISVllyS3g5c3R0N2V0VHYxRmU5bXlud0pEdWhZeUs0WVlkdGZIWElKVjBjaTJ4b0lDbWR5NWtYaWJvSWxkN2RwVkxuWFZvX2kyZ2RNeGt0Ynd4enFmcU5BIIEC; bili_jct=1a488c1a0c065e7c52748a3b9effd54c; sid=4hv352dx; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTE5NzEzMTEsImlhdCI6MTcxMTcxMjA1MSwicGx0IjotMX0.-sNIdSjptHmJN4OuPUIZicxtSyFKEtBTH78qR5Yn2Pg; bili_ticket_expires=1711971251; PVID=1",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
response = requests.get(url=url,headers=headers)
return response
def GetVideoInfo():
# 视频播放页的链接地址
link ='https://www.bilibili.com/video/BV1GJ411v7a3/?spm_id_from=333.999.0.0&vd_source=ae60a9753538f205a553937d75ee728d'
# 调用发送请求的函数
response = get_response(link)
# 获取响应的文本数据
html = response.text
# 提取数据内容 使用正则表达式
# 音视频信息得到
info = re.findall('<script>window.__playinfo__=(.*?)</script>',html)[0]
# 把json字符串转为json字典
json_data = json.loads(info)
# 音频链接
audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
# 视频链接
video_url = json_data['data']['dash']['video'][0]['baseUrl']
title = re.findall('<h1 data-title="(.*?)" title="(.*?)" class="video-title',html)[0][0]
print(title)
print(audio_url)
print(video_url)
# 获取视频标题
return title,audio_url,video_url
def save(title,audio_url,video_url):
audio_content = get_response(url=audio_url).content
video_content = get_response(url=video_url).content
with open('videobilibili\\'+title+'.mp3',mode='wb') as audio:
audio.write(audio_content)
with open('videobilibili\\'+title+'.mp4',mode='wb') as video:
video.write(video_content)
'''把音频和视频合成一个'''
if __name__ == '__main__':
title,audio_url,video_url = GetVideoInfo()
save(title,audio_url,video_url)
三、别人提供的b站视频详情页
在链接的bilibili前加个i,获得视频详情,包括评论信息
原链接:去大理骑单车环洱海,130公里2天半独行之旅_哔哩哔哩_bilibili
改链接:去大理骑单车环洱海,130公里2天半独行之旅-去大理骑单车环洱海,130公里2天半独行之旅-爱哔哩(B站视频、音频mp3解析下载站)
访问弹幕链接即可获得: