##我的网盘/博客素材/爬取b站视频
import requests
import re
import json
import subprocess
def get_response(html_url):
headers = {
'Referer': 'https://www.bilibili.com/video/BV1A44y1t7xt',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
response = requests.get(url=html_url,headers=headers)
return response
def get_video_info(html_url):
response = get_response(html_url)
title = re.findall('</script> <title data-vue-meta="true">(.*?)_哔哩哔哩_bilibili</title> ',response.text)[0].replace(' ','')
html_data = re.findall('<script>window\.__playinfo__=(.*?)</script>',response.text)[0]
#正则出来的数据是列表,[0]索引取字符串
#字符串转json字典
json_data = json.loads(html_data)
#pprint.pprint(json_data)
audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
video_url = json_data['data']['dash']['video'][0]['baseUrl']
video_info =[title,audio_url,video_url]
return video_info
# print(title)
# print(audio_url)
# print(video_url)
def save(title,audio_url,video_url):
audio_content = get_response(audio_url).content
video_content = get_response(video_url).content
with open(title + '.mp3',mode='wb') as f:
f.write(audio_content)
with open(title +'.mp4',mode='wb') as f:
f.write(video_content)
print('视频内容保存完成~')
def merge_data(video_name):
print("视频合成开始", video_name)
#video_name = video_name_replace(' ','')
COMMAND = f'ffmpeg -i {video_name}.mp4 -i {video_name}.mp3 -c:v copy -c:a aac -strict experimental {video_name}output.mp4'
subprocess.Popen(COMMAND,shell =True)
print('视频合成结束', video_name)
url = 'https://www.bilibili.com/video/BV1A44y1t7xt'
#response = get_response(url)
# print(response.text)
video_info = get_video_info(url)
save(video_info[0],video_info[1],video_info[2])
merge_data(video_info[0])
暂无评论