import requests
import json
import re
import os
from pprint import pprint
from moviepy.editor import *url = 'https://api.bilibili.com/pgc/view/web/season?ep_id=322903'
#设置请求头
header={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.43","Referer":"https://www.bilibili.com/bangumi/play/ep322903?from_spmid=666.4.feed.9", #设置防盗链"Cookie":"xxxxxx"
}resp = requests.get(url=url,headers=header)
print(resp.status_code)
json_data = resp.json()
#pprint(json_data)
node_all_list = []node_all = json_data['result']['episodes']
for node in node_all:#pprint(node)node_dic = {}node_dic['aid'] = node['aid']node_dic['cid'] = node['cid']node_dic['ep_id'] = node['ep_id']node_dic['share_copy'] = node['share_copy']node_dic['long_title'] = node['long_title']node_all_list.append(node_dic)breakprint('node_all_list',node_all_list)link_url ='https://api.bilibili.com/pgc/player/web/v2/playurl'for node in node_all_list:data = {'support_multi_audio': 'true','avid':node['aid'],'cid':node['cid'],'qn': '80','fnver': '0','fnval': '4048','fourk': '1','gaia_source':'','from_client':'BROWSER','ep_id': node['ep_id'],'session': 'af516338d69941203fd631e0f5d44f32','drm_tech_type': '2'}resp = requests.get(url=link_url,params=data,headers=header)print(resp.status_code)link_data = resp.json()pprint(link_data)#音频视频链接audio_url = link_data['result']['video_info']['dash']['audio'][0]['baseUrl']video_url = link_data['result']['video_info']['dash']['video'][0]['baseUrl']biaoti = node['share_copy']resp1 = requests.get(url=video_url, headers=header)with open(biaoti + '.mp4', mode='wb') as f:f.write(resp1.content)print('audio_url', audio_url)resp2 = requests.get(url=audio_url, headers=header)with open(biaoti + '.mp3', mode='wb') as f:f.write(resp2.content)# 视频音频文件组合video_path = biaoti + '.mp4'audio_path = biaoti + '.mp3'# 提取音轨audio = AudioFileClip(audio_path)# 读入视频video = VideoFileClip(video_path)# 将音轨合并到视频中video = video.set_audio(audio)# 输出video.write_videofile(f"{biaoti}(含音频).mp4")# 将涉及MP3MP4清除os.remove(video_path)os.remove(audio_path)print(biaoti,'爬取完成')break