首先,你需要知道b站正在播放的视频是分为视频加音频结合起来的,所以你需要爬取一段视频和一段音频。那么问题来了,怎么获取到视频文件和音频地址,单纯的F12网络里面的媒体(这种常规手段)是无法获取到内容的,所以我们需要看Xhr和Fetch这两个api带给我们的数据(这里对这两个api不做过多的解释)
着手开始研究这个fetch/xhr可以发现前面比较多文件就是原视频的音频和视频,这边有个小的技巧zhe'li就是选前面的序列号,这样视频和音频的地址也就出来了。后面就很好解决了
import requests
import re
from moviepy.editor import *
# 使用传统的自己查地址需要在引导头中加入refer来对应自己账户中的cookie,
header={'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'
,'Referer':
'https://www.bilibili.com/video/BV1UC411G7p3/?spm_id_from=333.1007.tianma.4-2-12.click&vd_source=fdc073a44533b9aaa0e8a4240d112d9f'
}
# 视频
video_url='https://cn-hncs-cu-01-09.bilivideo.com/upgcxcode/21/48/1506914821/1506914821-1-100026.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1713278363&gen=playurlv2&os=bcache&oi=0&trid=00002b4493de89a94c5b8f3c6bf898967923u&mid=1017756797&platform=pc&upsig=69705bdda0c5b9effac706d960ea4687&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&cdnid=34209&bvc=vod&nettype=0&orderid=0,3&buvid=C5BC861E-3400-B301-47E8-FF8B24ECA3C546528infoc&build=0&f=u_0_0&agrr=0&bw=129028&logo=80000000'
response=requests.get(video_url,headers=header)
with open('b站视频.mp4',mode='wb') as f:
f.write(response.content)
print(response.content)
# 音频
audio_url='https://cn-hncs-cu-01-09.bilivideo.com/upgcxcode/21/48/1506914821/1506914821-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1713278363&gen=playurlv2&os=bcache&oi=0&trid=00002b4493de89a94c5b8f3c6bf898967923u&mid=1017756797&platform=pc&upsig=1ecf832fd9590a0fcd66e3f9ab1bfe91&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&cdnid=34209&bvc=vod&nettype=0&orderid=0,3&buvid=C5BC861E-3400-B301-47E8-FF8B24ECA3C546528infoc&build=0&f=u_0_0&agrr=0&bw=15109&logo=80000000'
response1=requests.get(audio_url,headers=header)
with open('b站视频.mp3',mode='wb') as f:
f.write(response1.content)
print(response1.content)
这里需要注意的是需要加入refer到header中,这样refer信息中的的cookie才可以对应数据源从而获取到后端传过来的数据,
完成上一步之后,我们打开自己工作目录,可以发现生成了mp4视频和mp3音频,但是这个时候没有视频中是没有音频的,所以我们需要使用到一个模块movie.py库来将这两个音视频进行结合,附上代码
# 将视频和音频结合在一起
video_data=VideoFileClip('Z:/pycharm程序文件夹/哔哩哔哩/b站视频.mp4')
audio_data=AudioFileClip('Z:/pycharm程序文件夹/哔哩哔哩/b站视频.mp3')
final_video=video_data.set_audio(audio_data)
final_video.write_videofile('Z:/pycharm程序文件夹/哔哩哔哩/(音频+视频)b站视频.mp4')
怎么样听我讲解完整个爬取过程,是不是觉得有点“拨开云雾见青天”的感觉,最后完整的代码附上,不理解的码友可以自己尝试:
import requests
import re
from moviepy.editor import *
# 使用传统的自己查地址需要在引导头中加入refer来对应自己账户中的cookie,
header={'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'
,'Referer':
'https://www.bilibili.com/video/BV1UC411G7p3/?spm_id_from=333.1007.tianma.4-2-12.click&vd_source=fdc073a44533b9aaa0e8a4240d112d9f'
}
# 视频
video_url='https://cn-hncs-cu-01-09.bilivideo.com/upgcxcode/21/48/1506914821/1506914821-1-100026.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1713278363&gen=playurlv2&os=bcache&oi=0&trid=00002b4493de89a94c5b8f3c6bf898967923u&mid=1017756797&platform=pc&upsig=69705bdda0c5b9effac706d960ea4687&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&cdnid=34209&bvc=vod&nettype=0&orderid=0,3&buvid=C5BC861E-3400-B301-47E8-FF8B24ECA3C546528infoc&build=0&f=u_0_0&agrr=0&bw=129028&logo=80000000'
response=requests.get(video_url,headers=header)
with open('b站视频.mp4',mode='wb') as f:
f.write(response.content)
print(response.content)
# 音频
audio_url='https://cn-hncs-cu-01-09.bilivideo.com/upgcxcode/21/48/1506914821/1506914821-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1713278363&gen=playurlv2&os=bcache&oi=0&trid=00002b4493de89a94c5b8f3c6bf898967923u&mid=1017756797&platform=pc&upsig=1ecf832fd9590a0fcd66e3f9ab1bfe91&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&cdnid=34209&bvc=vod&nettype=0&orderid=0,3&buvid=C5BC861E-3400-B301-47E8-FF8B24ECA3C546528infoc&build=0&f=u_0_0&agrr=0&bw=15109&logo=80000000'
response1=requests.get(audio_url,headers=header)
with open('b站视频.mp3',mode='wb') as f:
f.write(response1.content)
print(response1.content)
# 将视频和音频结合在一起
video_data=VideoFileClip('Z:/pycharm程序文件夹/哔哩哔哩/b站视频.mp4')
audio_data=AudioFileClip('Z:/pycharm程序文件夹/哔哩哔哩/b站视频.mp3')
final_video=video_data.set_audio(audio_data)
final_video.write_videofile('Z:/pycharm程序文件夹/哔哩哔哩/(音频+视频)b站视频.mp4')