采用tkinter模块搭建GUI界面
代码如下:
import requests
from tkinter import *
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'}
def ts_url():
url = entry.get()
url_text = requests.get(url, headers=header).text
url_base1 = url.split('/')[2]
url_base2 = 'http://' + url_base1 + '/'
ts_name = re.findall('#EXTINF:(.*),\n(.*)\n', url_text)
ts_nameList = []
for name in ts_name:
ts_nameList.append(name[1])
for j in ts_nameList:
name = j.split('/')[-1]
text1.insert(END, '片段:{},正在下载...'.format(name))
text1.see(END)
text1.update()
url_ts = url_base2 + j
res = requests.get(url_ts, headers=header).content
with open('download\\%s' % name, 'wb') as f: # 需要在同目录下新建download文件夹
f.write(res)
text1.insert(END, '片段:{},下载完毕...'.format(name))
text1.see(END)
text1.update()
root = Tk()
root.title('m3u8解析')
root.geometry('604x400')
label = Label(root, text='请输入链接:', font=('Source Han Sans', 14, 'normal'))
# label.grid(row=0, column=0, sticky=E) # sticky 对齐方式 NEWS上下左右
# label.pack(side='top') # side 有top,bottom,left, right
label.place(x=0, y=4, width=104, height=24)
entry = Entry(root, font=('Source Han Sans', 14))
entry.place(x=104, y=4, width=500, height=24)
text1 = Listbox(root, font=('Source Han Sans', 14))
text1.place(x=0, y=32, width=400, height=300)
text2 = Listbox(root, font=('Source Han Sans', 14))
text2.place(x=404, y=32, width=200, height=300)
button = Button(root, text='开始下载', activebackground='blue', bg='yellow', font=('Source Han Sans', 14, 'normal'), command=ts_url) # command
button.place(x=0, y=336, width=100, height=24)
button1 = Button(root, text='退出程序', activebackground='red', bg='yellow', font=('Source Han Sans', 14, 'normal'), command=root.quit)
button1.place(x=504, y=336, width=100, height=24)
root.mainloop()
使用界面如下:
(https://github.com/jhhnet/imagebed/blob/master/E:%5C%E5%9B%BE%E7%89%87%5CimageBed38.jpg)
存在的问题,
就是单线程下载每一个片段都会导致界面卡顿,而且下载速度很慢,准备下一部分学习使用多进程或者多线程