才学了tkinter,感觉好炫酷的样子。但做的有点简陋,还有很多需要改进的地方。大家一起学习,一起进步呀!
from tkinter import * from prettytable import PrettyTable import os import re import requests import json import time import hashlib from urllib.parse import quote root = Tk() root.title('开心听音乐') root.geometry('720x520') lb = Label(root, text='亲!务必选择下载接口哦!', font=('楷体', 52), fg='red') lb.place(relx=0.3, rely=0.1, relheight=0.2, relwidth=0.2) lb.pack() title = Label(root, text='请输入歌名或歌手:', font=('楷体', 12), fg='red') title.place(relx=0.29, rely=0.22, relheight=0.055, relwidth=0.1) txt = Text(root) txt.place(relx=0.25, rely=0.3, relheight=0.4, relwidth=0.5) txt2 = Text(root) txt2.place(relx=0.25, rely=0.8, relheight=0.07, relwidth=0.5) which = Label(root, text='要下载哪一首歌:', font=('楷体', 12), fg='red') which.place(relx=0.29, rely=0.725, relheight=0.042, relwidth=0.1) downs = Entry(root) downs.place(relx=0.4, rely=0.725, relheight=0.04, relwidth=0.05) inp = Entry(root) inp.place(relx=0.4, rely=0.22, relheight=0.05, relwidth=0.2) def search(): dic = { 1: '酷我音乐', 2: '酷狗音乐'} data = dic.get(var.get()) if data == '酷我音乐': name = inp.get() namecode = quote(name) url = f'https://kuwo.cn/api/www/search/searchMusicBykeyWord?key={namecode}&pn=1&rn=30&httpsStatus=1' referer = f'https://kuwo.cn/search/list?key={namecode}' headers = {'csrf': 'FIAFFQIJH9G', 'Cookie': '_ga=GA1.2.1423499245.1668256225; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1668256135,1669972665,1669985480,1670163002; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1670163017; kw_token=FIAFFQIJH9G', 'Host': 'kuwo.cn', 'Referer': f'{referer}', 'user-agent': 'Mozilla/5.0 (Windows NTme/107.0.0.0 Safari/537.36 Edg/107.0.1418.26'} respose = requests.get(url=url, headers=headers).text dic2 = json.loads(respose)['data']['list'] table = PrettyTable() for s, li in enumerate(dic2): singer = li['artist'] name_ = li['name'] nnn = re.sub(r'[&,;$]', '', name_) sss = re.sub(r'[&,;]', '', singer) table.field_names = ['序号', '歌名', '歌手'] table.add_row([s + 1, nnn.replace('nbsp', ' '), sss.replace('nbsp', ' ')]) txt.insert(END, '{}'.format(table)) elif data == '酷狗音乐': name = inp.get() text = [ "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt", "appid=1014", "bitrate=0", "callback=callback123", "clienttime={}".format(int(time.time() * 1000)), "clientver=1000", "dfid=4XSnWz14ZQos2PYFIl2MiDLH", "filter=10", "inputtype=0", "iscorrection=1", "isfuzzy=0", "keyword={}".format(name), "mid=8a6709b0f4f0674f12dabeb3a710313a", "page=1", "pagesize=30", "platform=WebFilter", "privilege_filter=0", "srcappid=2919", "token=", "userid=0", "uuid=8a6709b0f4f0674f12dabeb3a710313a", "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt" ] data = "".join(text) # 变成字符串 md5 = hashlib.md5(data.encode(encoding='utf-8')).hexdigest() # md5加密 url = "https://complexsearch.kugou.com/v2/search/song" headers = { "referer": "https://www.kugou.com/", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" } params = { "callback": "callback123", "srcappid": "2919", "clientver": "1000", "clienttime": int(time.time() * 1000), "mid": "8a6709b0f4f0674f12dabeb3a710313a", "uuid": "8a6709b0f4f0674f12dabeb3a710313a", "dfid": "4XSnWz14ZQos2PYFIl2MiDLH", "keyword": f"{name}", "page": "1", "pagesize": "30", "bitrate": "0", "isfuzzy": "0", "inputtype": "0", "platform": "WebFilter", "userid": "0", "iscorrection": "1", "privilege_filter": "0", "filter": "10", "token": "", "appid": "1014", "signature": md5 } lll = json.loads(requests.get(url=url, headers=headers, params=params).text[12:-2]) kkk = lll['data']['lists'] table = PrettyTable() for s, li in enumerate(kkk): table.field_names = ['序号', '歌名', '歌手'] AlbumName = li['SongName'] singername = li['SingerName'] table.add_row([s + 1, AlbumName, singername]) txt.insert(END, '{}'.format(table)) else: txt.insert(END, '{}'.format('meiy')) def last(): dic = {1: '酷我音乐', 2: '酷狗音乐'} data = dic.get(var.get()) if data == '酷我音乐': name = inp.get() namecode = quote(name) url = f'https://kuwo.cn/api/www/search/searchMusicBykeyWord?key={namecode}&pn=1&rn=30&httpsStatus=1' referer = f'https://kuwo.cn/search/list?key={namecode}' headers = {'csrf': 'FIAFFQIJH9G', 'Cookie': '_ga=GA1.2.1423499245.1668256225; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1668256135,1669972665,1669985480,1670163002; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1670163017; kw_token=FIAFFQIJH9G', 'Host': 'kuwo.cn', 'Referer': f'{referer}', 'user-agent': 'Mozilla/5.0 (Windows NTme/107.0.0.0 Safari/537.36 Edg/107.0.1418.26'} respose = requests.get(url=url, headers=headers).text dic2 = json.loads(respose)['data']['list'] num = downs.get() kkop = dic2[int(num) - 1]['name'] kkkop = re.sub(r'[&,;$]', ' ', kkop) kkkopp = kkkop.replace('nbsp', '') opp = dic2[int(num) - 1]['rid'] urls = f'https://kuwo.cn/api/v1/www/music/playUrl?mid={opp}&type=mp3&httpsStatus=1&reqId=42ce60a1-743a-11ed-b07e-23cc131feebb&br=2000' music_ = requests.get(url=urls, headers=headers).text kkk = json.loads(music_)['data']['url'] # MP3下载地址 date_ = requests.get(url=kkk).content path_ = 'D:/音乐/' if not os.path.exists(path_): os.makedirs(path_) with open(path_ + f'{kkkopp}.mp3', 'wb') as f: f.write(date_) txt2.insert(END, '{}下载成功啦下载成功啦!'.format(kkkopp)) elif data == '酷狗音乐': name = inp.get() num = downs.get() text = [ "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt", "appid=1014", "bitrate=0", "callback=callback123", "clienttime={}".format(int(time.time() * 1000)), "clientver=1000", "dfid=4XSnWz14ZQos2PYFIl2MiDLH", "filter=10", "inputtype=0", "iscorrection=1", "isfuzzy=0", "keyword={}".format(name), "mid=8a6709b0f4f0674f12dabeb3a710313a", "page=1", "pagesize=30", "platform=WebFilter", "privilege_filter=0", "srcappid=2919", "token=", "userid=0", "uuid=8a6709b0f4f0674f12dabeb3a710313a", "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt" ] data = "".join(text) # 变成字符串 md5 = hashlib.md5(data.encode(encoding='utf-8')).hexdigest() # md5加密 url = "https://complexsearch.kugou.com/v2/search/song" headers = { "referer": "https://www.kugou.com/", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" } params = { "callback": "callback123", "srcappid": "2919", "clientver": "1000", "clienttime": int(time.time() * 1000), "mid": "8a6709b0f4f0674f12dabeb3a710313a", "uuid": "8a6709b0f4f0674f12dabeb3a710313a", "dfid": "4XSnWz14ZQos2PYFIl2MiDLH", "keyword": f"{name}", "page": "1", "pagesize": "30", "bitrate": "0", "isfuzzy": "0", "inputtype": "0", "platform": "WebFilter", "userid": "0", "iscorrection": "1", "privilege_filter": "0", "filter": "10", "token": "", "appid": "1014", "signature": md5 } lll = json.loads(requests.get(url=url, headers=headers, params=params).text[12:-2]) kkk = lll['data']['lists'] ID = lll['data']['lists'][int(num) - 1]['EMixSongID'] name = lll['data']['lists'][int(num) - 1]['SongName'] urls = f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191015294419033165485_1674051666168&dfid=4XSnWz14ZQos2PYFIl2MiDLH&appid=1014&mid=8a6709b0f4f0674f12dabeb3a710313a&platid=4&encode_album_audio_id={ID}&_=1674051666169' params = { "r": "play/getdata", "callback": "jQuery19101351666471912658_1674051302167", "dfid": "4XSnWz14ZQos2PYFIl2MiDLH", "appid": "1014", "mid": "8a6709b0f4f0674f12dabeb3a710313a", "platid": "4", "encode_album_audio_id": f"{ID}", "_": "1674051302168" } respons = json.loads(requests.get(url=urls, headers=headers, params=params).text[41:-2]) last = respons['data']['play_url'] downlode = requests.get(url=last, headers=headers).content with open('D:/音乐/' + f'{name}.mp3', 'wb') as sp: sp.write(downlode) txt2.insert(END, '{}下载成功啦下载成功啦!'.format(name)) else: pass def quits(): root.quit() var = IntVar() bt2 = Radiobutton(root, text='酷我音乐', variable=var, value=1) bt2.place(relx=0.3, rely=0.1, relheight=0.1, relwidth=0.2) bt3 = Radiobutton(root, text='酷狗音乐', variable=var, value=2) bt3.place(relx=0.5, rely=0.1, relheight=0.1, relwidth=0.2) btn4 = Button(root, text='搜索', fg='red', command=search) btn4.place(relx=0.63, rely=0.22, relheight=0.05, relwidth=0.1) btn5 = Button(root, text='下载', fg='red', command=last) btn5.place(relx=0.48, rely=0.728, relheight=0.038, relwidth=0.1) btn6 = Button(root, text='退出', fg='red', command=quits) btn6.place(relx=0.6, rely=0.728, relheight=0.038, relwidth=0.1) root.state('zoomed') root.mainloop()
标签:初试,tkinter,get,url,text,data,headers,root From: https://www.cnblogs.com/wxd501/p/17080790.html