python????????????b??????????
编辑:jimmy
日期: 2024/12/23 浏览:1 次
????
??????????????????????????????????????Web????bilibili??????????????????????????????????????????????Python????????b????????????????????????????????????????????????
??????????https://www.bilibili.com/video/BV1Fy4y1D7XS
??????b?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ffmpeg ???????????????????????????????????????????????????????????????????????????????? ffmpeg
??????????????????????????????????bin
????????????????
Python??????????????????requests
??re
??json
??subprocess
??os
????????
??????url
????????????????????headers
????????????????????????????
????????????F12??????????????????????????????????????????????????????????????
??????????????????head
??????????script
????????????????????????????
????????????????????403??????????????????????
??????????????????Request Headers ??????????????referer??????????????????????????????referer????????????????????????????bp????
Forward??????????????????????
????????????????????????????????
????????
????requests????????????????????????????????header??referer??????????????????????????????????????????????????????????????????Response??????????????????????????
??????????
import requestsheaders = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36","referer": "https://message.bilibili.com/"}def send_request(url): response = requests.get(url=url, headers=headers) #????get?????????????? return responsehtml_data = send_request("https://www.bilibili.com/video/BV1Qy4y147H1").textprint(html_data)
??????????
????????
????????????????HTML??json????????????????????????????????????????????????
title??????????????????head????
????????????????????????????
title = re.findall('<title data-vue-meta="true">(.*?)</title>',html_data)[0].replace("_???????? (?b-?b)???? ????~-bilibili",""
????????????????json????????
??????????????????????????????????????????????
json_data = re.findall(r'<script>window.__playinfo__=(.*?)</script>',html_data)[0] json_data = json.loads(json_data) #???? JSON ?????????? Python ???????????????? audio_url = json_data["data"]["dash"]["audio"][0]["backupUrl"][0] video_url = json_data["data"]["dash"]["video"][0]["backupUrl"][0]
??????????
import requestsimport reimport jsonimport pprintheaders = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36","referer": "https://message.bilibili.com/"}def send_request(url): response = requests.get(url=url, headers=headers) return responsedef get_video_data(html_data): title = re.findall('<title data-vue-meta="true">(.*?)</title>',html_data)[0].replace("_???????? (?b-?b)???? ????~-bilibili","") json_data = re.findall(r'<script>window.__playinfo__=(.*?)</script>',html_data)[0] json_data = json.loads(json_data) #pprint.pprint(json_data) audio_url = json_data["data"]["dash"]["audio"][0]["backupUrl"][0] video_url = json_data["data"]["dash"]["video"][0]["backupUrl"][0] video_data = [title, audio_url, video_url] return video_datahtml_data = send_request("https://www.bilibili.com/video/BV1Qy4y147H1").textvideo_data = get_video_data(html_data)for item in video_data: print(item)
??????????
????????
????????????????????????????????????????
??????????
import requestsimport reimport jsonheaders = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36","referer": "https://message.bilibili.com/"}def send_request(url): response = requests.get(url=url, headers=headers) return responsedef get_video_data(html_data): title = re.findall('<title data-vue-meta="true">(.*?)</title>',html_data)[0].replace("_???????? (?b-?b)???? ????~-bilibili","") json_data = re.findall(r'<script>window.__playinfo__=(.*?)</script>',html_data)[0] json_data = json.loads(json_data) audio_url = json_data["data"]["dash"]["audio"][0]["backupUrl"][0] video_url = json_data["data"]["dash"]["video"][0]["backupUrl"][0] video_data = [title, audio_url, video_url] return video_datadef save_data(file_name,audio_url,video_url): print("???????? " + file_name + "??????...") audio_data = send_request(audio_url).content print("???????? " + file_name + "????????") print("???????? " + file_name + "??????...") video_data = send_request(video_url).content print("???????? " + file_name + "????????") with open(file_name + ".mp3", "wb") as f: f.write(audio_data) with open(file_name + ".mp4", "wb") as f: f.write(video_data)html_data = send_request("https://www.bilibili.com/video/BV1Qy4y147H1").textvideo_data = get_video_data(html_data)save_data(video_data[0],video_data[1],video_data[2])
??????????
??????????
??????????????????????????????????????????????????????????????????????????????ffmpeg????????????????????????????????????????????????????????????????????1.mp3??1.mp4??????????????????????????????????????)
??????????
import requestsimport reimport jsonimport subprocessimport osheaders = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36","referer": "https://message.bilibili.com/"}def send_request(url): response = requests.get(url=url, headers=headers) return responsedef get_video_data(html_data): title = re.findall('<title data-vue-meta="true">(.*?)</title>',html_data)[0].replace("_???????? (?b-?b)???? ????~-bilibili","") json_data = re.findall(r'<script>window.__playinfo__=(.*?)</script>',html_data)[0] json_data = json.loads(json_data) audio_url = json_data["data"]["dash"]["audio"][0]["backupUrl"][0] video_url = json_data["data"]["dash"]["video"][0]["backupUrl"][0] video_data = [title, audio_url, video_url] return video_datadef save_data(file_name,audio_url,video_url): print("???????? " + file_name + "??????...") audio_data = send_request(audio_url).content print("???????? " + file_name + "????????") print("???????? " + file_name + "??????...") video_data = send_request(video_url).content print("???????? " + file_name + "????????") with open(file_name + ".mp3", "wb") as f: f.write(audio_data) with open(file_name + ".mp4", "wb") as f: f.write(video_data)def merge_data(video_name): os.rename(video_name + ".mp3","1.mp3") os.rename(video_name + ".mp4","1.mp4") print("???????? " + video_name + "??????...") subprocess.call("ffmpeg -i 1.mp4 -i 1.mp3 -c:v copy -c:a aac -strict experimental output.mp4", shell=True) os.rename("output.mp4", video_name + ".mp4") os.remove("1.mp3") os.remove("1.mp4") print("???????? " + video_name + "????????")html_data = send_request("https://www.bilibili.com/video/BV1Qy4y147H1").textvideo_data = get_video_data(html_data)save_data(video_data[0],video_data[1],video_data[2])merge_data(video_data[0])
??????????
??????????????????????????????
????????
# -*- coding : utf-8 -*-# home.php?mod=space&uid=238618 : 2021/3/21 16:11# home.php?mod=space&uid=686208 : wawyw# home.php?mod=space&uid=267492 : bilibili_video.py# home.php?mod=space&uid=371834 : PyCharmimport requestsimport reimport jsonimport subprocessimport osheaders = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36","referer": "https://message.bilibili.com/"}def send_request(url): response = requests.get(url=url, headers=headers) return responsedef get_video_data(html_data): title = re.findall('<title data-vue-meta="true">(.*?)</title>',html_data)[0].replace("_???????? (?b-?b)???? ????~-bilibili","") json_data = re.findall(r'<script>window.__playinfo__=(.*?)</script>',html_data)[0] json_data = json.loads(json_data) audio_url = json_data["data"]["dash"]["audio"][0]["backupUrl"][0] video_url = json_data["data"]["dash"]["video"][0]["backupUrl"][0] video_data = [title, audio_url, video_url] return video_datadef save_data(file_name,audio_url,video_url): print("???????? " + file_name + "??????...") audio_data = send_request(audio_url).content print("???????? " + file_name + "????????") print("???????? " + file_name + "??????...") video_data = send_request(video_url).content print("???????? " + file_name + "????????") with open(file_name + ".mp3", "wb") as f: f.write(audio_data) with open(file_name + ".mp4", "wb") as f: f.write(video_data)def merge_data(video_name): os.rename(video_name + ".mp3","1.mp3") os.rename(video_name + ".mp4","1.mp4") print("???????? " + video_name + "??????...") subprocess.call("ffmpeg -i 1.mp4 -i 1.mp3 -c:v copy -c:a aac -strict experimental output.mp4", shell=True) os.rename("output.mp4", video_name + ".mp4") os.remove("1.mp3") os.remove("1.mp4") print("???????? " + video_name + "????????")def main(): url = input("????bilibili????????????????????????") html_data = send_request(url).text video_data = get_video_data(html_data) save_data(video_data[0],video_data[1],video_data[2]) merge_data(video_data[0])if __name__ == "__main__": main()
??????
??????????????????????????????B??UP????????????????????????????????????????????????????????????????~
??????exe
????????????????Pyinstaller????????cmd????pip????
pip install pyinstaller
????????ffmpeg??py????????????????????????
????ffmpeg????????????????????????????????????????????????????????????????????
import requestsimport reimport jsonimport subprocessimport osimport shutilheaders = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36","referer": "https://message.bilibili.com/"}def send_request(url): response = requests.get(url=url, headers=headers) return responsedef get_video_data(html_data): title = re.findall('<title data-vue-meta="true">(.*?)</title>',html_data)[0].replace("_???????? (?b-?b)???? ????~-bilibili","") json_data = re.findall(r'<script>window.__playinfo__=(.*?)</script>',html_data)[0] json_data = json.loads(json_data) audio_url = json_data["data"]["dash"]["audio"][0]["backupUrl"][0] video_url = json_data["data"]["dash"]["video"][0]["backupUrl"][0] video_data = [title, audio_url, video_url] return video_datadef save_data(file_name,audio_url,video_url): print("???????? " + file_name + "??????...") audio_data = send_request(audio_url).content print("???????? " + file_name + "????????") print("???????? " + file_name + "??????...") video_data = send_request(video_url).content print("???????? " + file_name + "????????") with open(file_name + ".mp3", "wb") as f: f.write(audio_data) with open(file_name + ".mp4", "wb") as f: f.write(video_data)def merge_data(video_name): os.rename(video_name + ".mp3","1.mp3") os.rename(video_name + ".mp4","1.mp4") shutil.move("1.mp3","ffmpeg/bin/1.mp3") shutil.move("1.mp4","ffmpeg/bin/1.mp4") print("???????? " + video_name + "??????...") os.chdir("ffmpeg/bin/") subprocess.call("ffmpeg -i 1.mp4 -i 1.mp3 -c:v copy -c:a aac -strict experimental output.mp4", shell=True) os.rename("output.mp4", video_name + ".mp4") os.remove("1.mp3") os.remove("1.mp4") shutil.move("%s.mp4"%video_name,"../../%s.mp4"%video_name) print("???????? " + video_name + "????????")def main(): url = input("????bilibili????????????????????????\n") html_data = send_request(url).text video_data = get_video_data(html_data) save_data(video_data[0],video_data[1],video_data[2]) merge_data(video_data[0])if __name__ == "__main__": main()
??????????cmd??????????????????????????????????????????
Pyinstall -F -i bilibili.ico bilibili_video_download.py
??????-i bilibili.ico
??????????????????????????????????
??????????????????????????????????????????????????dist????????????????????????????bilibili_video_download
??exe????????????????????????????????????????????????exe????????????????????????ffmpeg????????????
????????exe??????????????????URL??????????
??????????
??????????????????????????????????????????????????????????????????????????????bilibili_video_download.exe
?????????????????????????????????? ??
??????https://pan.baidu.com/s/1P1451Mmg2FRQBnieSrUCQg
????????1234
??????????????????????????????App??????????????