MDT -一款批量下载音乐工具脚本

@Evileyesaint  2020 - 09 - 05

前言:

MDT (MusicDownloadTool)是一款批量下载音乐工具,其原理是访问音乐网站播放音乐的时候,会得到一个音乐文件或者得到XHR包,通过解析就可以得到音乐文件。

更新:

  • MDT v1.0(2020/9/5):

    • 目前仅支持网易云音乐歌单,其他音乐暂时不支持
    • 由于网易云限制,目前只能爬取歌单前十首音乐
    • 支持下载会员音乐

使用

基于Python编写,请先下载Python,然后打开cmd下载相应依赖:

pip install requests
pip install BeautifulSoup
pip install lxml

然后下载源码,再通过cmd打开文件所在目录(cd xxx),运行下列代码,输入歌单ID即可:

python MDT.py

源码:

import requests,os,re
from bs4 import BeautifulSoup

def getID():
    listID_wy = input('请输入歌单ID:')
    lenID=len(listID_wy)
    if listID_wy=='':
        print('你好像什么东西都没输入哎ε=(´ο`*)))')
        return 0, False
    elif 15>lenID>5:
        return listID_wy,True
    elif lenID>15:
        print('你输入貌似有点多哎,该不是输入链接了叭?')
        return 0, False
    else:
        print('才这么点怎么够[○・`Д´・ ○]?')
        return 0, False

def getmusiclist_wy(listID_wy):
    try:
        headers_wy = {
            'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed- 
      exchange;v=b3;q=0.9',
            'referer':'https://music.163.com/?from=infinity',
            'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 
                          Safari/537.36',
        }
        url_wy = 'http://music.163.com/playlist?id='+str(listID_wy)
        session_get = requests.session()
        res_wy = session_get.get(url_wy,headers=headers_wy)
        bs = BeautifulSoup(res_wy.content, 'lxml') 
        list_main = bs.find('ul', {'class': 'f-hide'}) 
        alllist = list_main.find_all('a')
        global mlt
        mlt = str(bs.find('h2', {'class': 'f-ff2 f-brk'}).text)
        musicID = []
        musicname = []
        print('歌单名称:'+str(mlt))
        for music in alllist:
            Mname = music.text
            newname='《'+str(Mname)+'》'
            music_ID = str(music['href']).replace('/song?id=', '')
            musicID.append(int(music_ID))
            musicname.append(Mname)
            print('歌名:{:<50} ID:{:}'.format(newname,music_ID)) 
        return musicID,musicname
    except:
        print('歌单貌似不存在,请检查下ID是否正确owo')
def downloadmusic(MID,path):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36'
    }
    DL = "http://music.163.com/song/media/outer/url?id="
    urlDL=DL+str(MID)+".mp3"
    music_get = requests.get(urlDL, headers=headers)
    music_get.raise_for_status()
    with open(path,"wb") as musicfile:
        musicfile.write(music_get.content)
    musicfile.close()
    print("done")
    

def main():
    ID, owo = getID()
    if owo:
        musicID,musicname = getmusiclist_wy(ID)
        if os.path.exists(mlt):
            print('===================!!!歌曲目录文件夹已存在!!!===================')
        else:
            os.mkdir(mlt)
            print('==================已创建歌曲目录文件夹,正在执行下载==================')
            
            for ID,name in zip(musicID,musicname):
                print(name+'正在下载...',end='')
                downloadmusic(ID,mlt+'/'+name+'.mp3')
            print('======================下载完毕✿✿ヽ(°▽°)ノ✿======================')
        
    else:
        return

if __name__=='__main__':
    main()

截图:

截图

源文件下载:

MDT v1.0


添加新评论