python下载网易云音乐中某个歌单中的全部音乐

以下面这个歌单为例:下载赵雷的所有音乐。其实我们只需获取这个歌单中所有音乐的id。

 我们查找一下歌曲的id信息:

这样就找到了歌曲的id信息:

 但是这样是拿不到的,我们要找的源码其实是在这:

 

接下来要做的就是用程序获取这些id:

1.请求本歌单的网址https://music.163.com/playlist?id=98332770(注意:要将\#号去掉)

 这里才是我们要请求的网址:

1import requests 2import time 3from bs4 import BeautifulSoup 4 5headers = { 6 'Host': 'music.163.com', 7 'Referer': 'https://music.163.com/', 8 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0', 9 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' 10} 11 12url="https://music.163.com/playlist?id=98332770" 13 14response = requests.get(url=url, headers=headers) 15html=response.content.decode(encoding="utf-8") 16#print(html) 17soup = BeautifulSoup(html, 'lxml') 18results = soup.find('ul',{'class':'f-hide'}) 19results=results.find_all('a') 20print(results) 21for music in results: 22 print(music.text, music['href']) 23

到这里,已经拿到了歌曲名及歌曲的id。

接下来是下载:

网易云歌曲一般会有一个外链,专门用于下载音乐音频的,以赵雷的歌曲《成都》为例,《成都》的外链URL是:http://music.163.com/song/media/outer/url?id=436514312.mp3,其中436514312这个数字是《成都》这首歌曲的ID。也就是说只要我们将歌曲的ID传入到外链URL中,就可以找到该歌曲的下载地址。

完整代码:

1import requests 2import time 3from bs4 import BeautifulSoup 4import urllib.request 5 6headers = { 7 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0', 8} 9 10url="https://music.163.com/playlist?id=98332770" 11 12response = requests.get(url=url, headers=headers) 13html=response.content.decode(encoding="utf-8") 14#print(html) 15soup = BeautifulSoup(html, 'lxml') 16results = soup.find('ul',{'class':'f-hide'}) 17results=results.find_all('a') 18print(results) 19for music in results: 20 #print(music.text, music['href']) 21 #下载歌曲 22 music_url="http://music.163.com/song/media/outer/url?id={}.mp3".format(music['href'].split("=")[1]) 23 print(music_url) 24 urllib.request.urlretrieve(music_url,music.text+'.mp3') 25 26 27

运行结果:

 

 

改进版:直接输入歌单的URL即可!!

完整代码:

 

1from tkinter import * 2import requests 3from bs4 import BeautifulSoup 4from urllib.request import urlretrieve 5 6 7def download(): 8 url = entry.get() 9 new_url = url.replace('/#', '') 10 header = { 11 'Host': 'music.163.com', 12 'Referer': 'https://music.163.com/', 13 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0' 14 } 15 res = requests.get(new_url, headers=header).text 16 r = BeautifulSoup(res, "html.parser") 17 music_dict = {} 18 result = r.find('ul', {'class', 'f-hide'}).find_all('a') 19 for music in result: 20 #print(music) 21 music_id = music.get('href').strip('/song?id=') 22 print(music_id) 23 music_name = music.text 24 music_dict[music_id] = music_name 25 for song_id in music_dict: 26 song_url = "http://music.163.com/song/media/outer/url?id=%s" % song_id 27 print(song_url) 28 path = r" %s.mp3" % music_dict[song_id] 29 30 # 添加数据 31 text.insert(END, "正在下载:%s" % music_dict[song_id]) 32 text.see(END) 33 text.update() 34 35 urlretrieve(song_url, path) 36 37 38root = Tk() 39root.title("网易云音乐下载器") 40root.geometry("550x400+550+230") 41 42label = Label(root, text="歌单URL", font=('宋体', 15)) 43label.grid() 44 45entry = Entry(root, font=('微软雅黑', 20)) 46entry.grid(row=0, column=1) 47 48text = Listbox(root, font=("微软雅黑", 15), width=45, height=10) 49text.grid(row=1, columnspan=2) 50 51button = Button(root, text="开始下载", font=("微软雅黑", 15), command=download) 52button.grid(row=2, column=0, sticky=W) 53 54button1 = Button(root, text="退出", font=("微软雅黑", 15), command=root.quit) 55button1.grid(row=2, column=1, sticky=E) 56 57 58mainloop() 59 60

 

代码交流 2021