嗨喽,大家好呀~这里是爱看美女的茜茜呐
第三方模块:
- requests >>> pip install requests
如何安装python第三方模块:
-
win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车
-
在pycharm中点击Terminal(终端) 输入安装命令
环境介绍:
-
python 3.8 解释器
-
pycharm 编辑器
-
ffmpeg.exe
👇 👇 👇 更多精彩机密、教程,尽在下方,赶紧点击了解吧~
python源码、视频教程、插件安装教程、资料我都准备好了,直接在文末名片自取就可
案例思路
一. 需求分析
如何采集单个的视频
视频在哪里?
window.playinfo=
二. 代码实现
-
核心编程
-
爬虫开发
-
数据分析
-
网站开发
代码展示
导入模块
'''
python资料获取看这里噢!! 小编 V:Pytho8987(记得好友验证备注:6 否则可能不通过)
即可获取:文章源码/教程/资料/解答等福利,还有不错的视频学习教程和PDF电子书!
'''
import requests
import re
import json
import subprocess
import os
if not os.path.exists('data'):
os.mkdir('data')
模拟伪装
headers = {
'cookie': "buvid3=355AA300-6A61-04E5-A05C-E891D886F69632716infoc; b_nut=1675085932; i-wanna-go-back=-1; _uuid=387EA3810-FBF5-E92C-827E-2510B578C5B9A33232infoc; buvid4=15C69C98-F6A7-EC6A-872F-E69C1840DD6D33724-023013021-1pW1w45e5fZS9RtebDiGZw%3D%3D; nostalgia_conf=-1; rpdid=|(kmJY|k))lY0J'uY~l|)lmY|; b_ut=5; is-2022-channel=1; buvid_fp_plain=undefined; CURRENT_BLACKGAP=0; LIVE_BUVID=AUTO3216755179681630; header_theme_version=CLOSE; CURRENT_PID=17897430-d93d-11ed-a1f4-675e4c96ff79; FEED_LIVE_VERSION=V8; CURRENT_QUALITY=80; fingerprint=58d6d808ef27a6225c943be7ca980284; buvid_fp=58d6d808ef27a6225c943be7ca980284; enable_web_push=DISABLE; CURRENT_FNVAL=4048; SESSDATA=0f019744%2C1717673066%2Ca41c0%2Ac2CjArLmPZFHNFg3B5H60pjRwiqJSLXDG8l2Pb_74Q11o8NmBWyKegdnFb6ivxUL255pwSVjRoaXFXVmFoRlFXY3VCRTAybEpud2ltaXFkRzZXQ25uZ3h0VGxrdGg3bWcxQ2hJN3d4VEZQRjRRTnd5cUx2TmJfUUdlWVZocVRfb281QnJHSklrTkJ3IIEC; bili_jct=f2a37b8a7351e9987d90f80d72dab593; DedeUserID=422789639; DedeUserID__ckMd5=fc4901c78719b545; home_feed_column=5; browser_resolution=1920-963; bp_video_offset_422789639=873101572088791044; sid=81ais7cc; b_lsid=C410CB11B_18C81D7BCD4; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDMyNDQ2NTEsImlhdCI6MTcwMjk4NTM5MSwicGx0IjotMX0.IXqgmieQsWabRzC7N4FfWaox2gffTBRi6tcnfRzlq4U; bili_ticket_expires=1703244591; PVID=2",
'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',
'Referer': 'https://www.bilibili.com/video/BV1oK411C7fT/?vd_source=8f216a44bce0dbe14e3447c34c2ab3e2'
}
请求链接
url = 'https://www.bilibili.com/video/BV1oK411C7fT/?vd_source=8f216a44bce0dbe14e3447c34c2ab3e2'
- 发送请求
response = requests.get(url, headers=headers)
. 获取数据
html_data = response.text
- 解析数据
'''
python资料获取看这里噢!! 小编 V:Pytho8987(记得好友验证备注:6 否则可能不通过)
即可获取:文章源码/教程/资料/解答等福利,还有不错的视频学习教程和PDF电子书!
'''
INITIAL_STATE = re.findall('window.__INITIAL_STATE__=(.*?);\(function\(\)', html_data)[0]
initial_state = json.loads(INITIAL_STATE)
episodes = initial_state['sectionsInfo']['sections'][0]['episodes']
for episode in episodes:
bvid = episode['bvid']
b_url = f'https://www.bilibili.com/video/{bvid}'
b_html = requests.get(b_url, headers=headers).text
play_info = re.findall('window.__playinfo__=(.*?)</script>', b_html)[0]
play_info_dict = json.loads(play_info)
video_url = play_info_dict['data']['dash']['video'][0]['baseUrl']
audio_url = play_info_dict['data']['dash']['audio'][0]['baseUrl']
- 保存数据
print(video_url)
video_data = requests.get(video_url, headers=headers).content
with open(f'data/{bvid}.mp4', mode='wb') as f:
f.write(video_data)
print(audio_url)
audio_data = requests.get(audio_url, headers=headers).content
with open(f'data/{bvid}.mp3', mode='wb') as f:
f.write(audio_data)
- 合并音视频
ffmpeg = rf'ffmpeg -i data/{bvid}.mp4 -i data/{bvid}.mp3 -acodec copy -vcodec copy data/{bvid+"-out.mp4"}'
subprocess.run(ffmpeg)
os.remove(f'data/{bvid}.mp4')
os.remove(f'data/{bvid}.mp3')
尾语
感谢你观看我的文章呐~本次航班到这里就结束啦 🛬
希望本篇文章有对你带来帮助 🎉,有学习到一点知识~
躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。