闲来无事试着写一下,代码如下(打包程序在最后):
[Python] 纯文本查看 复制代码 import requests
import urllib.request
import os
def download_music():
music_url = input('请输入歌曲链接:')
# 获取链接最后一个'/'后面的字符串
last_slash_index = music_url.rfind('/')
music_id = music_url[last_slash_index + 1:]
music_name = input('请输入要保存的歌曲名:')
headers = {
"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"
}
url_base = "https://www.kuwo.cn/api/v1/www/music/playUrl?mid=%s&type=flac&httpsStatus=1&reqId=80b33650-8a62-11ed-a069-8d99eba73f2a"
url = url_base % music_id
response = requests.get(url, headers=headers)
result = response.json()
play_url = result.get("data").get("url")
# 获取桌面的位置
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
file_path = os.path.join(desktop_path, music_name + '.mp3')
# 下载文件
urllib.request.urlretrieve(play_url, file_path)
if __name__ == '__main__':
try:
download_music()
except Exception as e:
print("下载失败:", e)
else:
print("下载成功!(默认下载到桌面)")
运行截图:
exe文件下载链接:https://wwvt.lanzoum.com/i9kcO0jwwxfc密码:fe70 |