音乐播放器
陈鑫磊的博客
 
文章 标签
4

Copyright © 2020-2021 陈鑫磊
RSS 随机音乐
北京时间: : )正在加载....
Powered by Gridea | Theme: Fog
载入天数...
载入时分秒...
总访问量:  |   访问人数:

网易云音乐音乐下载器

  热度: loading...

代码小白,多多关注
exe文件下载地址
仓库地址,什么都没有
使用方法
在文件根目录创建music文件夹,歌曲保存在里面

#coding = gbk
import requests
from urllib.request import quote
from fake_useragent import UserAgent
import io
import sys
ua = UserAgent()
user=ua.chrome
print("当前UA:"+user)
headers = {"User-Agent":user}

#用户输入
MusicName=input("请输入歌曲名称:")
print("正在搜索"+MusicName)
# 进行拼音转换
#ret2 = quote(MusicName, encoding="gbk")
ret2 = quote(MusicName.encode('utf-8'))
#ret2 = quote(MusicName.encode('utf-8'), encoding="gbk")

print(ret2)

#歌曲搜索功能
url = 'https://v1.hitokoto.cn/nm/search/'+ret2+"?limit=1"
#url = "http://music.163.com/api/search/pc?s="+ret2+"limit=1"
apiget = requests.get(url, allow_redirects=True)
apiget.encoding = 'utf-8'
apidata = apiget.json()
print(apidata)


#解析id
if apiget.status_code == 200:
    #id=apidata['result']['songs'][0]['artists'][0]['id']
    id=apidata['result']['songs'][0]['id']
    print(id)
    #下载
    url="http://music.163.com/song/media/outer/url?id="+str(id)+".mp3"
    r = requests.get(url,headers=headers)
    
    reditList = r.history#可以看出获取的是一个地址序列
    print(f'获取重定向最终的url:{reditList[len(reditList)-1].headers["location"]}')
    #with open("music\\"+str(id)+".mp3", "wb") as code:
    with open("music\\"+MusicName+".mp3", "wb") as code:
        code.write(r.content)
    print("下载成功")