杰米工坊 Design By 杰米

????

??????????????????????????????????????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 ????????????????????????????

python????????????b??????????

????????????????????403??????????????????????

python????????????b??????????

??????????????????Request Headers ??????????????referer??????????????????????????????referer????????????????????????????bp????

python????????????b??????????

Forward??????????????????????

python????????????b??????????

????????????????????????????????

python????????????b??????????

????????

????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)

??????????

python????????????b??????????

????????

????????????????HTML??json????????????????????????????????????????????????

title??????????????????head????

python????????????b??????????

????????????????????????????

title = re.findall('<title data-vue-meta="true">(.*?)</title>',html_data)[0].replace("_???????? (?b-?b)???? ????~-bilibili",""

????????????????json????????

python????????????b??????????

??????????????????????????????????????????????

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)

??????????

python????????????b??????????

????????

????????????????????????????????????????

??????????

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])

??????????

python????????????b??????????

python????????????b??????????

??????????

??????????????????????????????????????????????????????????????????????????????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])

??????????

python????????????b??????????

??????????????????????????????

python????????????b??????????

????????

# -*- 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()

??????

python????????????b??????????

??????????????????????????????B??UP????????????????????????????????????????????????????????????????~


??????exe

????????????????Pyinstaller????????cmd????pip????

pip install pyinstaller