import os标签:python,问津,获取,video,url,key,download,path,audio From: https://www.cnblogs.com/zyb-luckey/p/18187709
import requests
import json
import re
from bs4 import BeautifulSoup
from qiniu import Auth, put_file
class BilibiliVideoAudio:
def __init__(self, bid, download_path='download'):
self.bid = bid
self.headers = {
"referer": "https://www.bilibili.com",
"origin": "https://www.bilibili.com",
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36',
'Accept-Encoding': 'identity'
}
self.download_path = download_path
# 创建下载路径
if not os.path.exists(download_path):
os.makedirs(download_path)
def get_video_audio(self):
# 构造视频链接并发送请求获取页面内容
url = f'https://www.bilibili.com/video/{self.bid}?vd_source=6659c978e0a0ff11c82fae0ae309aabe'
content = requests.get(url, headers=self.headers).content.decode('utf-8')
soup = BeautifulSoup(content, 'html.parser')
# 获取视频标题
meta_tag = soup.head.find('meta', attrs={'name': 'title'})
title = meta_tag['content']
# 获取视频和音频链接
pattern = r'window\.__playinfo__=({.*?})\s*</script>'
json_data = re.findall(pattern, content)[0]
data = json.loads(json_data)
video_url = data['data']['dash']['video'][0]['base_url']
audio_url = data['data']['dash']['audio'][0]['base_url']
return {
'title': title,
'video_url': video_url,
'audio_url': audio_url
}
def download_video_audio(self, url, filename):
# 对文件名进行清理,去除不合规字符
filename = self.sanitize_filename(filename)
try:
# 发送请求下载视频或音频文件
resp = requests.get(url, headers=self.headers).content
download_path = os.path.join(self.download_path, filename)
with open(download_path, mode='wb') as file:
file.write(resp)
print("{:*^30}".format(f"下载完成:{filename}"))
return download_path # 返回下载文件的本地路径
except Exception as e:
print(e)
def sanitize_filename(self, filename):
# 定义不合规字符的正则表达式
invalid_chars_regex = r'[\"*<>?\\|/:,]'
# 替换不合规字符为空格
sanitized_filename = re.sub(invalid_chars_regex, ' ', filename)
return sanitized_filename
def upload_to_qiniu(access_key, secret_key, bucket_name, local_file_path, key=None):
q = Auth(access_key, secret_key)
token = q.upload_token(bucket_name, key, 3600)
ret, info = put_file(token, key, local_file_path)
if info.status_code == 200:
print("文件上传成功!")
else:
print("文件上传失败:", info.error)
def main():
bids = ["BV1bL4y1E71a"] # 视频的bid,可以修改为其他视频的bid
for bid in bids:
bilibili = BilibiliVideoAudio(bid)
video_audio_info = bilibili.get_video_audio()
title = video_audio_info['title']
video_url = video_audio_info['video_url']
audio_url = video_audio_info['audio_url']
video_path = bilibili.download_video_audio(video_url, f"{title}.mp4") # 下载视频
audio_path = bilibili.download_video_audio(audio_url, f"{title}.mp3") # 下载音频
# 上传视频和音频到七牛云
upload_to_qiniu(access_key='lObvgTkNUTdmLkeRH-fSuc3jeHEfdAgtxXzzvIbW',
secret_key='YjnzJuCH_6-qWb8aQ5nGTIqsVDFywRN_tgdrkOF_',
bucket_name='shanjupayss',
local_file_path=video_path,
key=f"{title}.mp4")
upload_to_qiniu(access_key='lObvgTkNUTdmLkeRH-fSuc3jeHEfdAgtxXzzvIbW',
secret_key='YjnzJuCH_6-qWb8aQ5nGTIqsVDFywRN_tgdrkOF_',
bucket_name='shanjupayss',
local_file_path=audio_path,
key=f"{title}.mp3")
if __name__ == "__main__":
main()