import json import requests import re url = 'https://www.bilibili.com/video/BV1H7421T7zx/?spm_id_from=333.337.search-card.all.click&vd_source=f6247aa12dae1ff1bce74ef0af381757' def GetResponse(url): headers = { 'accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / avif, image / webp, image / apng, * / *;q = 0.8, application / signed - exchange;v = b3;q = 0.7', 'accept - encoding': 'gzip, deflate', 'accept - language': 'zh - CN, zh;q = 0.9, en;q = 0.8, en - GB;q = 0.7, en - US;q = 0.6', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36', 'Referer':'https://search.bilibili.com/all?vt=44591831&keyword=%E8%B0%81%E7%94%B5%E5%90%89%E4%BB%96%E8%B0%B1&from_source=webtop_search&spm_id_from=333.1007&search_source=2' } response = requests.get(url = url,headers = headers) return response def GetVideoInfo(): response = GetResponse(url = url) html = response.text info = re.findall('', html)[0] json_data = json.loads(info) audio_url = json_data['data']['dash']['audio'][0]['baseUrl'] video_url = json_data['data']['dash']['video'][0]['baseUrl'] title = re.findall('(.*?)', html) if title: title = title[0] else: return None return (title, audio_url, video_url) def Save(title,audio_url,video_url): audio_content = GetResponse(url = audio_url).content video_content = GetResponse(url = video_url).content with open('bili\\' + title + '.mp3', mode = 'wb') as audio: audio.write(audio_content) with open('bili\\' + title + '.mp4', mode = 'wb') as video: video.write(video_content) if __name__ == '__main__': title,audio_url,video_url = GetVideoInfo() Save(title, audio_url, video_url)