Python: use tkinter to make a small music download software

Life is too short, I use Python

Usually the songs we download are in various formats of monsters and ghosts. If you want to download them and use them, you can only listen to them in the player. Who can stand it~


What is learning Python for?

Of course it solves the problem~

So I directly wrote a first-hand music download software, forced to save all mp3, so there is no limit~

What? Want Mane? If it doesn't exist, don't panic, you can download it all!

Show results

This is the software interface we are going to write today

Then we choose a song

High-heeled shoes look like they are going to be mane, so that's it~

It makes sense, I am speechless~

off topic...

Enter music ID


click to download

Check it out

We can package this file as an exe executable file, so that it can be sent to others~

About packaging, I will not go into details, you can see previous articles: Python packaging exe

code section

download section

def Download():
    music_info = input_va.get()

    if music_info.isdigit():
        link = f'https://***com/song?id={
      
      music_info}'
        headers = {
    
    
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
        }
        html_data = requests.get(url=link, headers=headers).text
        title = re.findall('<title>(.*?)</title>', html_data)[0]
        play_url = 'http://***com/song/media/outer/url?id=' + music_info
        music_content = requests.get(url=play_url, headers=headers).content
        file = 'music\\'
        if not os.path.exists(file):
            os.makedirs(file)
        with open(file + title + '.mp3', mode='wb') as f:
            f.write(music_content)
        tkinter.messagebox.showinfo(title='下载成功', message=f'{
      
      title}歌曲下载完成')
    else:
        tkinter.messagebox.showerror(title='警告', message='输入有问题')

Interface and function realization

Top_frame = tk.Frame(root)
Top_frame.pack()
img = tk.PhotoImage(file='img\\music.png')
tk.Label(Top_frame, image=img).pack(side=tk.LEFT, pady=10, padx=10)
tk.Label(Top_frame, text='全民音乐解析', font=('微软雅黑', 20), fg='#03b72b').pack(side=tk.LEFT, pady=10, padx=10)
tk.Label(Top_frame, text='歌曲免费下载', font=('微软雅黑', 20), fg='#0e90d2').pack(side=tk.LEFT, pady=10, padx=10)


type_frame = tk.Frame(root)
type_frame.pack()
click_va = tk.IntVar()
click_va.set(2)
tk.Radiobutton(type_frame, text='音乐名字', font=('黑体', 15), variable=click_va, value=1,
               ).pack(side=tk.LEFT, pady=10, padx=20)
tk.Radiobutton(type_frame, text='音乐ID', font=('黑体', 15), variable=click_va, value=2,
               ).pack(side=tk.LEFT, pady=10, padx=20)
tk.Radiobutton(type_frame, text='音乐地址', font=('黑体', 15), variable=click_va, value=3,
               ).pack(side=tk.LEFT, pady=10, padx=20)


input_frame = tk.Frame(root)
input_frame.pack()
input_va = tk.StringVar()
input_entry = tk.Entry(input_frame, relief='flat', width=50, justify='center', font=('微软雅黑', 15), textvariable=input_va)
input_entry.pack()


choose_frame = tk.Frame(root)
choose_frame.pack()
choose_click_va = tk.IntVar()
choose_click_va.set(1)
tk.Radiobutton(choose_frame, text='网易', font=('黑体', 12), variable=choose_click_va, value=1,
               ).pack(side=tk.LEFT, pady=10, padx=20)
tk.Radiobutton(choose_frame, text='企鹅', font=('黑体', 12), variable=choose_click_va, value=2,
               ).pack(side=tk.LEFT, pady=10, padx=20)
tk.Radiobutton(choose_frame, text='酷狗', font=('黑体', 12), variable=choose_click_va, value=3,
               ).pack(side=tk.LEFT, pady=10, padx=20)
tk.Radiobutton(choose_frame, text='酷我', font=('黑体', 12), variable=choose_click_va, value=4,
               ).pack(side=tk.LEFT, pady=10, padx=20)

Button_frame = tk.Frame(root)
Button_frame.pack()
tk.Button(Button_frame, text='VIP音乐搜索解析下载', font=('黑体', 20), bg='#0e90d2', fg='#ffffff', width=50, relief='flat', command=Download).pack(pady=10)
root.mainloop()

Okay, that's all for today's sharing, pick up the business card below the complete code~

Follow me and share more interesting knowledge~

Finally, a set of Python videos is recommended: 100 practical examples of Python projects

Guess you like

Origin blog.csdn.net/fei347795790/article/details/126959973