import requests import re # 视频对应Key值 id = "BV1dP4y1e7Zz" url = "https://www.bilibili.com/video/{id}/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=7b035b72f78efa1e2682479296eb889e".format(id = id) print(url) header = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", "referer": "https://www.bilibili.com/" } bili = requests.get(url = url, headers = header) # 格式化后,注意匹配格式问题! 需保持原始数据相同 # 视频url video_url = re.findall('"baseUrl":"(.*?)",', bili.text) # 视频标题 video_title = re.findall('property="og:title" content="(.*?)">', bili.text) print(video_title) video = requests.get(url = video_url[0], headers = header).content with open('./{video_title}.mp4'.format(video_title = video_title[0]),'wb') as f: f.write(video) f.close() |