网易云音乐下载


`import json
import requests
from bs4 import BeautifulSoup
import tkinter as tk
from tkinter import ttk #下拉列表
import webbrowser
import os
from threading import Thread

headers={

    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
}

ids = []#音乐id列表
gename=[]#歌曲名字列表
list_urls=[]#歌曲链接
geshou=[]#300歌手名字列表

def gname():

shuziurl = 'http://www.jianpuw.com/artist/'
shuzihtml = requests.get(shuziurl, headers=headers).content.decode('utf-8')
shuzi_html = shuzihtml
soup = BeautifulSoup(str(shuzi_html), "lxml")
li_shuzi = soup.findAll('a')
for i in range(2, len(li_shuzi)):
    geshou.append(li_shuzi[i].text)
print(geshou)

gname()

def sousuo():

for i in range(len(list_urls)):
    list_urls.pop(0)

for i in range(0,50):
    list_xial.delete(0)
name = en.get()
# name=str(name)
url = f"https://music.cyrilstudio.top/search?keywords={name}&limit=30"
# print(url)
try:
    datahtml = requests.get(url, headers=headers)
    html = datahtml.text
    data = json.loads(html)
    # print(data)
    list_data = data['result']['songs']
    # print(list_data)
    for i in list_data:
        id = i['id'] #音乐id
        ids.append(id)

        name = i['name'] #音乐名字
        gename.append(name)

        geshou = i['artists'][0]['name']#音乐歌手

        urls = f'http://music.163.com/song/media/outer/url?id={id}.mp3'
        list_urls.append(urls)
        print(id, name, geshou, urls)

        list_xial.insert(tk.END,str(name)+"——"+str(geshou))
    print(ids)
    print(list_urls)
except Exception as e:
    list_xial.insert(tk.END,'未找到该歌手或歌曲信息!')
    print("未找到该歌手或歌曲信息")

功能方法

def ggk(event):

var.set(xiala.get())
print(geshou.index(xiala.get()))

def xuanzhe(e):

c=e.widget
global list_index
list_index=c.curselection()[0]
print(c.curselection()[0])
xianshi_lb.config(text=list_xial.get(c.curselection()[0]))

def tiaozhuan():

try:
    url=list_urls[list_index]
    webbrowser.open(url)
except Exception as g:
    print(g)

def xiaz():

f="./Music/"
if not os.path.exists(f):
    os.makedirs(f)

url = list_urls[list_index]
xia=requests.get(url)
# print(xia.text)
with open(str(f+gename[list_index])+'.Mp3','wb') as x:
    x.write(xia.content)

线程

def xiancheng():

apps=Thread(target=sousuo)
apps.setDaemon(True)
apps.start()

def xiancheng2():

apps=Thread(target=xiaz)
apps.setDaemon(True)
apps.start()

可视化界面

app=tk.Tk()
app.title("网易云音乐下载")

布局大小

app.geometry('500x700')
app.resizable(width=False, height=False)

控件

sou_text=tk.Label(app,text='输入搜素歌手或歌名:')

var=tk.StringVar()
en=tk.Entry(app,textvariable=var,width=30,borderwidth = 3)

xiala = ttk.Combobox(app, width=10)
xiala['values'] = geshou

anniu=tk.Button(app,text='搜素',command=xiancheng,width=10,height=2)

list_xial=tk.Listbox(app,width=50,height=26,exportselection=False)

xianshi_lb=tk.Label(app,text='未选择音乐')

anniu2=tk.Button(app,text='跳转播放',command=tiaozhuan)

anniu3=tk.Button(app,text='下载音乐',command=xiancheng2)

事件绑定

xiala.bind("<<ComboboxSelected>>",ggk)

list_xial.bind("<<ListboxSelect>>",xuanzhe)

sou_text.pack()
sou_text.place(x=40,y=10)

en.pack()
en.place(x=155,y=40)

xiala.pack()
xiala.place(x=50,y=40)

anniu.pack()
anniu.place(x=389,y=30)

list_xial.pack()
list_xial.place(x=70,y=100)

anniu2.pack()
anniu2.place(x=150,y=620)

anniu3.place(x=260,y=620)

xianshi_lb.pack()
xianshi_lb.place(x=100,y=590)

if __name__=="__main__":

app.mainloop()

`

声明:木东蓝博客|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - 网易云音乐下载


学习新思想,争做新青年!