网页听歌的单曲循环
网页听歌的单曲循环

网页听歌的单曲循环

# 其实就是下载到本地哈哈
import requests
import re
import json
# 更改你要的url和你自己的cookie
url = ‘https://www.bilibili.com/video/BV1kC4y197tf/’
cookie = “”
headers = {
        “Referer”: “https://www.bilibili.com/video/BV1kC4y197tf/”,
        “User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36”,
        “Cookie”: cookie
}
# 发送请求
response = requests.get(url=url, headers=headers)
html = response.text
# 检查请求是否成功
# 将HTML保存到当前文件夹,可以用 Prettier 格式化代码方便分析
if response.status_code == 200:
    with open(‘output.html’, ‘w’, encoding=’utf-8′) as file:
        file.write(response.text)
    print(“HTML已保存”)
else:
    print(f”请求失败,状态码:{response.status_code}”)
# 提取视频标题
title = re.findall(‘title=”(.*?)”‘, html)[0]
# 提取视频信息
info = re.findall(‘window.__playinfo__=(.*?)</script>’, html)[0]
# print(type(info))
# 返回一个 Python 对象,具体类型取决于 info 中 JSON 数据的结构
# 如果 JSON 是一个对象(即由 {} 包围),它会转换为一个 字典
# 如果 JSON 是一个数组(即由 [] 包围),它会转换为一个 列表
# str -> dic
json_data = json.loads(info)
with open(“data.json”, “w”, encoding=”utf-8″) as file:
    json.dump(json_data, file, ensure_ascii=False, indent=4)
# print(type(json_data))
audio_data = json_data[“data”][“dash”][“audio”]
video_data = json_data[“data”][“dash”][“video”]
def get_max_bandwidth_url(input_data):
    max_bandwidth = -1
    max_base_url = “”
    if input_data:
        for input in input_data:
            bandwidth = input[‘bandwidth’]
            if bandwidth > max_bandwidth:
                max_bandwidth = bandwidth
                max_base_url = input[‘baseUrl’]
    if max_base_url:
        return max_base_url
    else:
        print(f”No baseUrl found in this {input_data}”)
audio_url = get_max_bandwidth_url(audio_data)
print(“Audio Url:”, audio_url)
video_url = get_max_bandwidth_url(video_data)
print(“Video Url:”, video_url)
# 保存数据
audio_content = requests.get(url=audio_url, headers=headers).content
with open(title + ‘.mp3′, mode=’wb’) as a:
    a.write(audio_content)
# video_content = requests.get(url=video_url, headers=headers).content
# with open(title + ‘.mp4′, mode=’wb’) as v:
#     v.write(video_content)

一条评论

发表回复