[Python] 纯文本查看 复制代码
# 导入模块
from tkinter import *
from tkinter.ttk import *
import requests
import jsonpath
import os
from urllib.request import urlretrieve
import tkinter.messagebox as msgbox
xz = ""
class WinGUI(Tk):
def __init__(self):
super().__init__()
self.__win()
self.tk_label_qsrgqm = self.__tk_label_qsrgqm()
self.tk_input_inputgqm = self.__tk_input_inputgqm()
self.tk_button_ss = self.__tk_button_ss()
self.tk_radio_button_netease = self.__tk_radio_button_netease()
self.tk_progressbar_xzjdt = self.__tk_progressbar_xzjdt()
self.tk_radio_button_qq = self.__tk_radio_button_qq()
self.tk_radio_button_kugou = self.__tk_radio_button_kugou()
self.tk_radio_button_kuwo = self.__tk_radio_button_kuwo()
self.tk_radio_button_baidu = self.__tk_radio_button_baidu()
self.tk_radio_button_1ting = self.__tk_radio_button_1ting()
self.tk_radio_button_lizhi = self.__tk_radio_button_lizhi()
self.tk_radio_button_qingting = self.__tk_radio_button_qingting()
self.tk_radio_button_ximalaya = self.__tk_radio_button_ximalaya()
self.tk_radio_button_5singyc = self.__tk_radio_button_5singyc()
self.tk_radio_button_5singfc = self.__tk_radio_button_5singfc()
self.tk_radio_button_kg = self.__tk_radio_button_kg()
self.tk_list_box_text = self.__tk_list_box_text()
self.tk_label_xzjd = self.__tk_label_xzjd()
self.tk_button_xz = self.__tk_button_xz()
self.tk_radio_button_migu = self.__tk_radio_button_migu()
def __win(self):
self.title("全网歌曲下载")
# 设置窗口大小、居中
width = 600
height = 500
screenwidth = self.winfo_screenwidth()
screenheight = self.winfo_screenheight()
geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
self.geometry(geometry)
self.resizable(width=False, height=False)
global xz
xz = StringVar()
xz.set("qq")
def __tk_label_qsrgqm(self):
label = Label(self, text="请输入要下载的歌曲名")
label.place(x=20, y=10, width=147, height=24)
return label
def __tk_input_inputgqm(self):
ipt = Entry(self)
ipt.place(x=180, y=10, width=312, height=24)
return ipt
def __tk_button_ss(self):
btn = Button(self, text="搜索")
btn.place(x=500, y=10, width=44, height=24)
return btn
def __tk_radio_button_netease(self):
rb = Radiobutton(self, text="网易",variable=xz,value='netease')
rb.place(x=20, y=50, width=60, height=24)
return rb
def __tk_progressbar_xzjdt(self):
progressbar = Progressbar(self, orient=HORIZONTAL)
progressbar.place(x=80, y=450, width=473, height=24)
return progressbar
def __tk_radio_button_qq(self):
rb = Radiobutton(self, text="QQ",variable=xz,value='qq')
rb.place(x=90, y=50, width=54, height=24)
return rb
def __tk_radio_button_kugou(self):
rb = Radiobutton(self, text="酷狗",variable=xz,value='kugou')
rb.place(x=150, y=50, width=61, height=24)
return rb
def __tk_radio_button_kuwo(self):
rb = Radiobutton(self, text="酷我",variable=xz,value='kuwo')
rb.place(x=220, y=50, width=57, height=24)
return rb
def __tk_radio_button_baidu(self):
rb = Radiobutton(self, text="千千",variable=xz,value='baidu')
rb.place(x=290, y=50, width=58, height=24)
return rb
def __tk_radio_button_1ting(self):
rb = Radiobutton(self, text="一听",variable=xz,value='1ting')
rb.place(x=360, y=50, width=61, height=24)
return rb
def __tk_radio_button_lizhi(self):
rb = Radiobutton(self, text="荔枝",variable=xz,value='lizhi')
rb.place(x=430, y=50, width=59, height=24)
return rb
def __tk_radio_button_qingting(self):
rb = Radiobutton(self, text="蜻蜓",variable=xz,value='qingting')
rb.place(x=500, y=50, width=65, height=24)
return rb
def __tk_radio_button_ximalaya(self):
rb = Radiobutton(self, text="喜马拉雅",variable=xz,value='ximalaya')
rb.place(x=20, y=90, width=96, height=24)
return rb
def __tk_radio_button_5singyc(self):
rb = Radiobutton(self, text="5sing原创",variable=xz,value='5singyc')
rb.place(x=130, y=90, width=99, height=24)
return rb
def __tk_radio_button_5singfc(self):
rb = Radiobutton(self, text="5sing翻唱",variable=xz,value='5singfc')
rb.place(x=240, y=90, width=99, height=24)
return rb
def __tk_radio_button_kg(self):
rb = Radiobutton(self, text="全民K歌",variable=xz,value='kg')
rb.place(x=350, y=90, width=99, height=24)
return rb
def __tk_list_box_text(self):
lb = Listbox(self)
lb.place(x=20, y=130, width=561, height=310)
return lb
def __tk_label_xzjd(self):
label = Label(self, text="下载进度")
label.place(x=10, y=450, width=59, height=24)
return label
def __tk_button_xz(self):
btn = Button(self, text="下载")
btn.place(x=550, y=10, width=43, height=24)
return btn
def __tk_radio_button_migu(self):
rb = Radiobutton(self, text="咪咕",variable=xz,value='migu')
rb.place(x=460, y=90, width=80, height=24)
return rb
class Win(WinGUI):
def __init__(self):
super().__init__()
self.__event_bind()
self.bind('<Return>', self.get_music_name)
# 2.功能实现""" 1.url 2.模拟浏览器请求 3.解析网页源代码 4.保存数据"""
def cbk(self,a, b, c):
'''回调函数
@a:已经下载的数据块
@b:数据块的大小
@c:远程文件的大小
'''
per = 100.0 * a * b / c
if per > 100:
per = 100
self.tk_progressbar_xzjdt['value'] =per
self.tk_progressbar_xzjdt.update()
print('%.2f%%' % per)
def song_download(self,url, title, author, jlrc):
# 创建文件夹
self.tk_button_ss.configure(state='disabled')
self.tk_button_xz.configure(state='disabled')
os.makedirs("music", exist_ok=True)
path = 'music\{}.mp3'.format(title)
lrcpath = 'music\{}.lrc'.format(title)
self.tk_list_box_text.insert(END, '歌曲:{0}-{1},正在下载...'.format(title, author))
# 文本框滑动
self.tk_list_box_text.see(END)
# 更新
self.tk_list_box_text.update()
# 下载
urlretrieve(url, path, self.cbk)
if os.path.exists(lrcpath): # 如果存在a/1.txt文件
r = lrcpath
file = open(r, 'a', encoding='utf-8') # a表示在原有文档下继续编辑
file.write(str(jlrc[0])) # 在文件中写入loss: 2
file.flush()
file.close()
else:
# r为路径,'a\\1.txt','\\'与'/'一致,在os.path.join()函数中,
# 每个逗号之间的元素都会被'\\'分开,无需添加分隔符
r = lrcpath
file = open(r, 'w', encoding='utf-8') # w表示在重新创建文件
file.write(str(jlrc[0])) # 在文件中写入loss: 2
file.flush()
file.close()
self.tk_list_box_text.insert(END, '下载完毕,{0}-{1},请试听'.format(title, author))
# 文本框滑动
self.tk_list_box_text.see(END)
# 更新
self.tk_list_box_text.update()
self.tk_button_ss["state"] = "enable"
self.tk_button_xz["state"] = "enable"
def down_music(self):
""" 搜索歌曲名称 :return: """
djg = self.tk_input_inputgqm.get()
djg = int(djg) - 1
name = self.tk_input_inputgqm.get()
platfrom = WinGUI.xz.get()
# name = '碧蓝的学姐与朱砂痣'
url = 'https://music.liuzhijin.cn/'
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
# 判断请求是异步还是同步
"x-requested-with": "XMLHttpRequest", }
param = {"input": name, "filter": "name", "type": platfrom, "page": 1, }
res = requests.post(url=url, data=param, headers=headers)
json_text = res.json()
# print(res)
title = jsonpath.jsonpath(json_text, '$..title')
author = jsonpath.jsonpath(json_text, '$..author')
url = jsonpath.jsonpath(json_text, '$..url')
jlrc = jsonpath.jsonpath(json_text, '$..lrc')
# print("jlrc",jlrc)
# print("json_text", json_text)
# print(url[djg], title[djg], author[djg],jlrc)
self.song_download(url[djg], title[djg], author[djg], jlrc)
def get_music_name(self, evt):
""" 搜索歌曲名称 :return: """
name = self.tk_input_inputgqm.get()
platfrom = xz.get()
self.tk_list_box_text.delete(0, END)
if name=="" or platfrom=="":
msgbox.showerror("完整性检测","请填写歌名或选择下载站点!")
print(platfrom)
# name = '碧蓝的学姐与朱砂痣'
url = 'https://music.liuzhijin.cn/'
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36",
# 判断请求是异步还是同步
"x-requested-with": "XMLHttpRequest", }
param = {"input": name, "filter": "name", "type": platfrom, "page": 1, }
res = requests.post(url=url, data=param, headers=headers)
json_text = res.json()
title = jsonpath.jsonpath(json_text, '$..title')
author = jsonpath.jsonpath(json_text, '$..author')
url = jsonpath.jsonpath(json_text, '$..url')
print(title, author, url)
ii = 0
for i in title:
# 插入文本框
strchuan = "第" + str(ii + 1) + "个---" + title[ii] + " " + author[ii] + " " + url[ii]
self.tk_list_box_text.insert(END, strchuan.format(title, author))
# 文本框滑动
self.tk_list_box_text.see(END)
# 更新
self.tk_list_box_text.update()
ii = ii + 1
def downthis(self, evt):
print("<Double-Button-1>事件未处理", evt)
w = evt.widget
print(w.index(w.curselection()))
value = w.get(w.curselection())
print(value)
""" 搜索歌曲名称 :return: """
djg = w.index(w.curselection())
name = self.tk_input_inputgqm.get()
platfrom = xz.get()
# name = '碧蓝的学姐与朱砂痣'
url = 'https://music.liuzhijin.cn/'
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
# 判断请求是异步还是同步
"x-requested-with": "XMLHttpRequest", }
param = {"input": name, "filter": "name", "type": platfrom, "page": 1, }
res = requests.post(url=url, data=param, headers=headers)
json_text = res.json()
# print(res)
title = jsonpath.jsonpath(json_text, '$..title')
author = jsonpath.jsonpath(json_text, '$..author')
url = jsonpath.jsonpath(json_text, '$..url')
jlrc = jsonpath.jsonpath(json_text, '$..lrc')
# print("jlrc",jlrc)
# print("json_text", json_text)
# print(url[djg], title[djg], author[djg],jlrc)
self.song_download(url[djg], title[djg], author[djg], jlrc)
def __event_bind(self):
self.tk_button_ss.bind('<Button>', self.get_music_name)
self.tk_button_xz.bind('<Button>', self.xzan)
self.tk_list_box_text.bind('<Double-Button-1>', self.downthis)
def xzan(self,evt):
print("下载啦")
msgbox.showerror('错误提示', '直接双击具体的歌单信息即可下载!')
if __name__ == "__main__":
win = Win()
win.mainloop()