文静 发表于 2021-9-3 10:21:21

小破站视频爬取-仅供学习使用

{:10_297:}
import requests
import re
import time
from lxml import etree
import json
from moviepy.editor import *

url = input('视频网址:')
headers = {
    'Referer': url,
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0'
}

res = requests.get(url,headers=headers)
html_data = etree.HTML(res.content.decode())
title = html_data.xpath('//*[@id="viewbox_report"]/h1/span/text()')
titles = title + "&"
# print(title)

response = res.text
pattern = '<script>window\.__playinfo__=(.*?)</script>'
list1 = re.findall(pattern,response,re.S)
list_json = json.loads(list1)
video_url = list_json['data']['dash']['video']['baseUrl']
audio_url = list_json['data']['dash']['audio']['baseUrl']
# print(video_url)
# print(audio_url)

video_res = requests.get(video_url,headers=headers)
audio_res = requests.get(audio_url,headers=headers)

with open('%s.mp4'%titles,'wb') as f:
    f.write(video_res.content)
with open('%s.mp3'%titles,'wb') as f:
    f.write(audio_res.content)
time.sleep(3)
# 读取视频文件 和读取音频文件
videoclip = VideoFileClip("%s.mp4"%titles)
Audioclip = AudioFileClip("%s.mp3"%titles)
# 给videoclip视频 添加音频Audioclip
video_data = videoclip.set_audio(Audioclip)
# 添加好了后 再输出为一个视频
video_data.write_videofile('%s.mp4'%(title))

helenfalcon 发表于 2021-9-6 10:44:57

感谢!
页: [1]
查看完整版本: 小破站视频爬取-仅供学习使用