python批量下载网易云音乐文件到本地

时间:2024-02-25 22:36:06

现在听歌大多数只支持在线听,下载要钱,没网络就白搭了。好吧,用技术手段解决免费、下载、批量等一些列问题

整个脚本的逻辑和流程是,把歌曲地址都存在一个txt中,然后循环每次取一条链接,分析链接对应歌曲的id和歌曲名,然后下载该歌曲,同时已歌曲名命名下载后的文件

 

 

 

网易云音乐客户端复制音乐地址,比如上面那首《踏山河》:https://music.163.com/song?id=1804320463&userid=502092978

歌曲地址内有可以看到id=1804320463,需要把这串数字分离出来,后面下载会用到,用正则分离出来

url=“https://music.163.com/song?id=1804320463&userid=502092978“”
song_id=re.findall(\'id=(.+?)&userid\',url)[0]

 

然后根据地址获取歌曲名称,这里使用的是xpath爬虫抓取数据

song_url=“https://music.163.com/song?id=1804320463&userid=502092978“”
response = requests.get(song_url,headers)
wb_data = response.text # 将页面转换成文档树
html = etree.HTML(wb_data)
music_name = html.xpath(\'//em[@class = "f-ff2"]/text()\')[0]

 

再就是下载歌曲,下载歌曲需要用到网易云的专属下载接口,http://music.163.com/song/media/outer/url?id=,id=后面即填入上面步骤分离出来的歌曲id

response = requests.get("http://music.163.com/song/media/outer/url?id="+song_id+".mp3")
        content = response.content
        with open(music_name+\'.mp3\', mode="wb") as f:
            f.write(content)
        print("下载"+"   "+music_name+"  "+"完成")

 

然后在脚本目录下创建一个txt文件,把歌曲地址复制到文本内(每行一条地址)

 

 

 为了方便观察每一步获取的数据,再每一步操作后面加入把数据打印出来,执行脚本

 

 

 

 

 

以上即是本案例的主要步骤和对应的脚本,至于读取txt文件等,就参考后面附上的所有脚本内容

同时,已经把脚本打包成exe文件,没有安装python的电脑环境也可以执行(加微信xiaobeishuwu发文件)

 

特别强调,不要下的太过分,仅仅是技术交流,商业牟利自负责任

 

 

 附全部脚本

import requests   #倒入requests库
from lxml import etree  #倒入lxml 库(没有这个库,pip install lxml安装)
import re

headers = {
    \'authority\': \'music.163.com\',
    \'pragma\': \'no-cache\',
    \'cache-control\': \'no-cache\',
    \'upgrade-insecure-requests\': \'1\',
    \'user-agent\': \'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36\',
    \'accept\': \'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\',
    \'sec-fetch-site\': \'same-origin\',
    \'sec-fetch-mode\': \'nested-navigate\',
    \'referer\': \'https://music.163.com/\',
    \'accept-encoding\': \'gzip, deflate, br\',
    \'accept-language\': \'zh-CN,zh;q=0.9\',
    \'cookie\': \'_iuqxldmzr_=32; _ntes_nuid=01c796eee1899b3e988043381c5fe3ed; WM_TID=bClwjpRRyMhFBRRUVVZ8eNGfcwSRUP4D; vjuids=91172de86.167ba393a03.0.751a7745c4db9; vjlast=1545017572.1572947033.22; mail_psc_fingerprint=73ad847ab9b5c4a03a9db849350e6c87; _ntes_nnid=01c796eee1899b3e988043381c5fe3ed,1606698119460; NMTID=00OIwuy7su1M7DopUdapOO9k7XQi98AAAF2Fqu0fA; _ga=GA1.2.1259495320.1607509476; P_INFO=zoulong1989@yeah.net|1607662298|0|mailyeah|00&99|hun&1606470131&mailyeah#hun&430100#10#0#0|&0|mailyeah|zoulong1989@yeah.net; ntes_kaola_ad=1; __guid=94650624.2334178747771955700.1621191366476.4607; WEVNSM=1.0.0; vinfo_n_f_l_n3=3d881b79189ff41c.1.12.1547520183001.1614838367331.1621988944652; WNMCID=ejsics.1623379617606.01.0; WM_NI=mdEEc6iEFx0kqT58oiR1ru7%2FkLYUA1PyRK6%2BTHLghGMimil%2BS5tVG%2FwgVucIvSbr8YLaHYx37EAxuzxT1dsFJEhDfawZAmKXmaade%2FrrEDFjQu3pQNMcaiAKD5mlJQ26TGI%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeb2ea6a8a889cd1cc49ed9a8fb6d85b879b8f84f862baaaa5acd669b49f99b9eb2af0fea7c3b92aafbc8ba6e23eab9b8a99b361b38cfbd9ee6392efbe96b625a99497b6c43b94a79eacb52198988488eb4d8fb7a38aef2191ebbcb7c625b5b99a96e243b68dae93bb5390bf88bafb39f59b8f99f872bc8cff85f24e8a9ba8a5e56bb5868f8eca54afbfaf85e825b2ae8ba5c56293abfd91d4468fbdaf8ac44188eba38dc95bf2ea9ca8cc37e2a3; playerid=78983598; JSESSIONID-WYYY=%2FRnux48Q%2FpfVFCwSG6v2vRBRWVgCV%2B6Sq6AI4NdRfU527Dz%2FaA74w3xExYMzsWMrMCOh4w6fvc%5CM3inN2WkZQ3gtAUt%5CHwj4rtxKYCdj6OaYHu%2FxAthJgtmzw%2BaHzKUSz36Vo45aOsuf1Hq2NTx0N5qs7p%5CPc%2B6KB6ZvkhG5HvzS62pi%3A1629883157132; monitor_count=7\',
}

soure = open(\'address.txt\', \'r\')
value = soure.readlines()
soure.close()

def download_music(value,headers):
    #读取歌曲地址
    for url in value:
        url=url[:-1]#去掉换行符,然后url后面会带个空格,访问不了链接

    #解析地址,获取歌曲名称和id
        song_id=re.findall(\'id=(.+?)&userid\',url)[0]
        print("歌曲id:"+song_id)
        song_url = "https://music.163.com/song?id="+song_id
        response = requests.get(song_url,headers=headers)
        wb_data = response.text # 将页面转换成文档树
        html = etree.HTML(wb_data)
        music_name = html.xpath(\'//em[@class = "f-ff2"]/text()\')[0]
        print("歌曲名称:"+music_name)
    #将歌曲名称和id传入,下载歌曲
        response = requests.get("http://music.163.com/song/media/outer/url?id="+song_id+".mp3",headers=hearders)
        content = response.content
        with open(music_name+\'.mp3\', mode="wb") as f:
            f.write(content)
        print("下载"+"   "+music_name+"  "+"完成")
download_music(value,headers)