用python写一个简易的下载网易云音乐歌单的脚本

运行后只需输入歌单的id即可

歌单id可通过分享歌单的链接获取

from requests import get
from json import loads
from multiprocessing.dummy import Pool
import os
import easygui as e
class Solve:
    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
            'Referer': 'https://music.163.com/'
        }


    def getid(self, list_id):  # 获取id
        url = 'https://api.imjad.cn/cloudmusic/?type=playlist&id=' + list_id
        text = loads(get(url, headers=self.headers).text)  # 获取返回值
        ids = list(map(lambda x: str(x['id']), text['playlist']['trackIds']))
        list_name = text['playlist']['name']
        return ids, list_name

    def getdetail(self, song_id):  # 获取歌曲名以及作者
        try:
            url = 'https://api.imjad.cn/cloudmusic/?type=detail&id=' + song_id
            text = loads(get(url).text)
            detail = (text['songs'][0]['name'], text['songs']
                      [0]['ar'][0]['name'], song_id)
            self.download(detail[2], detail[0], detail[1])  # 储存
        except:
            pass

    def download(self, id, name, artist):  # 下载
        if len(name) > 20:
            name = name[:20]

        path = name + '-' + artist + '.mp3'
        url = 'http://music.163.com/song/media/outer/url?id=' + id
        f = open(path, 'wb')
        print('downloading...', name)
        html = get(url, headers=self.headers,timeout = 5)
        f.write(html.content)
        f.close()



if __name__ == "__main__":
    cool = Solve()
    list_id = e.enterbox('请输入歌单id:')
    ids, list_name = cool.getid(list_id)
    pool = Pool()
    path = e.diropenbox('请选择存放地址')
    os.chdir(path)
    if not os.path.exists(list_name):
        try:
            os.mkdir(list_name)
        except:
            list_name = e.enterbox("请自行输入文件名")
            os.mkdir(list_name)
    os.chdir(list_name)
    pool.map(cool.getdetail, ids)
    pool.close()
    pool.join()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值