import os
import requests
import urllib.request
from bs4 import BeautifulSoup
from tkinter import *
def music_download():
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
if not os.path.exists('./网易云歌单/'):
os.mkdir('./网易云歌单/')
play_url = entry1.get()
s = requests.session()
response = s.get(play_url,headers=headers).content
soup =BeautifulSoup(response,'lxml')
music_data = soup.find('ul',class_='f-hide')
lists = []
for music in music_data.find_all('a'):
#print('{}:{}'.format(music.text,music['href']))
list = []
music_url = 'http://music.163.com/song/media/outer/url'+ music['href'][5:] + '.mp3'
print(music_url)
music_name = music.text
list.append(music_name)
list.append(music_url)
lists.append(list)
#print(lists)
os.chdir('.\网易云歌单')
b = os.getcwd()
print('当前目录变为为:', b)
for i in lists:
url = i[1]
name = i[0]
try:
print("正在下载",name)
text.insert(END, '歌曲:{},正在下载。。。'.format(name))
text.see(END)
header1 ={"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36"
}
#urllib.request.urlretrieve(url,'./网易云歌单/%s.mp3' % name)
res = requests.get(url,headers=header1)
with open(str(name)+'.mp3',"ab")as f:
f.write(res.content)
print('下载成功。。。')
text.insert(END, '下载完毕:{},可以去听听'.format(name))
text.see(END)
text.update()
except:
pass
主界面
root = Tk()
root.geometry('590x480+400+260')
root.title('网易云歌单下载器')
labell1 = Label(root,text = '请输入歌单的网页链接,记得去除#号哦',font=('微软雅黑',15))
labell1.grid()
entry1 = Entry(root,font=('微软雅黑',12))
entry1.grid(row=2,column=0)
text = Listbox(root, font=('微软雅黑', 16), width=40, height=10)
text.grid(row=3, columnspan=1)
b1 = Button(root, text='开始下载', font=('微软雅黑', 15),command=music_download)
b1.grid(row=4, column=0)
b2 = Button(root, text='退出程序', font=('微软雅黑', 15), command=root.quit)
b2.grid(row=4, column=1)
root.mainloop()