python 学习笔记
自制 tk 框架下载视频
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2020/10/19 15:39
@Author : Ping Wang
@Email : ping.wang@hrbmu.edu.cn
@Site :
@File :
@Software : PyCharm
"""
import os
import tkinter as tk
import re,urllib,requests
from bs4 import BeautifulSoup
from tkinter import filedialog,messagebox
import pyperclip
root = tk.Tk()
root.title('网易云音乐MV下载器')
root.geometry('700x400')
root.configure(bg='whitesmoke')
percent = None
def callbackfunc(blocknum, blocksize, totalsize):
global percent
"""
回调函数
@blocknum: 已经下载的数据块
@blocksize: 数据块的大小
@totalsize: 远程文件的大小
"""
percent = 100.0 * blocknum * blocksize / totalsize
if percent > 100:
percent = 100.0
canvas.coords(fill_line, (0, 0, 270 * percent / 100, 60))
label3_1['text'] = '%6.2f' % percent + '% '+'%.1f/%.1f M'%(blocknum * blocksize/1048576,totalsize/1048576)
root.update()
searchURL = downloadurl = hurl = murl = name = downloadpath = '','','','','',''
def submitPaperurl():
global searchURL,downloadurl,hurl,murl,name,downloadpath,HD,MD
try:
button5.destroy()
except:
None
# 获取网址
searchURL = EditText0.get(1.0, 'end')[:-1]
searchURL=searchURL[:22]+searchURL[24:]
# 爬取视频名称 ,视频下载网址
s = requests.get(searchURL).text
hurl=urllib.parse.unquote(re.search(r'hurl=(.*)&murl',s,re.M)[1])
murl=urllib.parse.unquote(re.search(r'murl=(.*)&auto',s,re.M)[1])
name=re.search(r'&trackName=(.*)&artistName=',s,re.M)[1]+"-"\
+re.search(r'&artistName=(.*)&resourceId=',s,re.M)[1]
chooseMD()
# 显示视频名字
EditText1.delete(1.0,'end')
EditText1.insert(1.0,name)
HD.destroy()
MD.destroy()
if hurl!='':
HD = tk.Radiobutton(frame21, text="高清", variable=var, value=1, command=chooseHD)
HD.pack()
if murl!='':
MD = tk.Radiobutton(frame21, text="标清", variable=var, value=2, command=chooseMD)
MD.pack()
root.update()
def chooseHD():
global downloadurl,downloadpath
'''
if hurl=='':
messagebox.showinfo(title='error', message='无高清下载链接',icon='error')
'''
downloadurl = hurl
downloadpath = os.getcwd()+'\\'+name+'-HD.mp4'
EditText4.delete(1.0,'end')
EditText4.insert(1.0,hurl)
root.update()
def chooseMD():
global downloadurl,downloadpath
downloadurl = murl
downloadpath = os.getcwd()+'\\'+name+'-MD.mp4'
EditText4.delete(1.0,'end')
EditText4.insert(1.0,murl)
root.update()
def copyDownloadurl():
pyperclip.copy(downloadurl)
def downloadMV():
global button5
urllib.request.urlretrieve(downloadurl, downloadpath,callbackfunc)
button5 = tk.Button(frame, text="播放", overrelief='sunken', bg='whitesmoke', activebackground='aliceblue',
activeforeground='blue', font=("微软雅黑", 9, 'bold'), command=openMV)
button5.pack(side='right', fill='x',padx=25)
def clearall():
EditText0.delete(1.0, 'end')
EditText1.delete(1.0, 'end')
canvas.coords(fill_line, (0, 0, 0, 60))
label3_1['text'] = '%6.2f' % 0 + '%'
def openMV():
os.startfile(downloadpath)
# 创建标题
label = tk.Label(root, text="网易云音乐MV下载器", bg='whitesmoke', font=("华文楷体", 25, 'bold'))
label.pack()
'''
button = tk.Button(frame, text="一键下载", overrelief='sunken', bg='whitesmoke', activebackground='aliceblue',
activeforeground='blue', font=("微软雅黑", 12, 'bold'), command=oneclick)
button.pack()
'''
frame = tk.Frame(root, bd=5, bg='whitesmoke')
frame.pack()
frame0 = tk.Frame(frame, bd=5, bg='whitesmoke')
frame0.pack(anchor='w')
label0 = tk.Label(frame0, text='输入视频网址(url):', bd=5, font=("微软雅黑", 10, 'bold'), bg='whitesmoke')
label0.pack(side='left', fill='x')
EditText0 = tk.Text(frame0, width=40, height=2)
EditText0.pack(side='left', fill='x')
button0 = tk.Button(frame0, text="提交", overrelief='sunken', bg='whitesmoke', activebackground='aliceblue',
activeforeground='blue', font=("微软雅黑", 9, 'bold'), command=submitPaperurl)
button0.pack(side='right', fill='x')
frame1 = tk.Frame(frame, bd=5, bg='whitesmoke')
frame1.pack(anchor='w')
label1 = tk.Label(frame1, text='视频名字 :', bd=5, font=("微软雅黑", 10, 'bold'), bg='whitesmoke')
label1.pack(side='left', fill='x',anchor='w')
EditText1 = tk.Text(frame1, width=40, height=2)
EditText1.pack(side='left', fill='x')
frame2 = tk.Frame(frame, bd=5, bg='whitesmoke')
frame2.pack(anchor='w')
label2 = tk.Label(frame2, text='选择下载清晰度 :', bd=5, font=("微软雅黑", 10, 'bold'), bg='whitesmoke')
label2.pack(side='left', fill='x')
frame21 = tk.Frame(frame2, bd=5, bg='whitesmoke')
frame21.pack(side='left', fill='x',padx=100)
var = tk.IntVar()
var.set(2)
HD = tk.Radiobutton(frame21, text=' ', variable=var, value=1, command=[])
HD.pack()
MD = tk.Radiobutton(frame21, text=' ', variable=var, value=2, command=[])
MD.pack()
button2 = tk.Button(frame2, text="下载", overrelief='sunken', bg='whitesmoke', activebackground='aliceblue',
activeforeground='blue', font=("微软雅黑", 9, 'bold'), command=downloadMV)
button2.pack(side='right', fill='x',padx=25)
frame4 = tk.Frame(frame, bd=5, bg='whitesmoke')
frame4.pack(anchor='w')
label4 = tk.Label(frame4, text='下载链接 :', bd=5, font=("微软雅黑", 10, 'bold'), bg='whitesmoke')
label4.pack(side='left', fill='x')
EditText4 = tk.Text(frame4, width=40, height=5)
EditText4.pack(side='left', fill='x')
button4 = tk.Button(frame4, text="复制到剪切板", overrelief='sunken', bg='whitesmoke', activebackground='aliceblue',
activeforeground='blue', font=("微软雅黑", 8, 'bold'), command=copyDownloadurl)
button4.pack(side='right', fill='x')
# 进度条框架
frame3 = tk.Frame(frame, bd=5, bg='whitesmoke')
frame3.pack(anchor='w')
label3 = tk.Label(frame3, text='下载进度 :', bd=10, bg='whitesmoke', font=("微软雅黑", 9, 'bold'))
label3.pack(side='left', fill='x')
canvas = tk.Canvas(frame3, width=270, height=20, bg="white")
canvas.pack(side='left', fill='x')
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
label3_1 = tk.Label(frame3, text='%6.2f' % 0 + '%', bd=5, bg='whitesmoke', font=("微软雅黑", 9, 'bold'))
label3_1.pack(side='right', fill='x')
root.mainloop()
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于