import tkinter as tk
from tkinter import END # 单独把这个申明是因为后面的往text控件插入内容会报错
import requests
import re
import time
import wget
class Douyin(object):
"""docstring for Womdows"""
def __init__(self):
self.tk=tk
self.window = tk.Tk()
self.show_text=tk.Text()
self.var_token = tk.StringVar()
def get_tar(self):
token = self.var_token.get()
time.sleep(0.5) # 停顿1s
if token =='':
return self.show_text.insert(END,'请输入一个地址\n')
if token in 'v.douyin.com':
pat = '(https://v.douyin.com/.*?/)'
url = re.compile(pat).findall(share)[0]# 正则匹配分享链接
else:
url=token
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3904.108 Safari/537.36'
}
r = requests.get(url, headers=headers)
pat = 'playAddr: "(.*?)",'
name_ = '<p class="name nowrap">(.*?)</p>'
info_ = '<p class="desc"(.*?)</p>'
play = re.compile(pat).findall(r.text)[0].replace("playwm", "play")#视频地址
name_a = re.compile(name_).findall(r.text)[0]#发布人作者
info_a = re.compile(info_).findall(r.text)[0].replace('>','')#发布详情
headers = {
'user-agent': 'Android',
}
r = requests.get(play, headers=headers, allow_redirects=False)
geturl = r.headers['location']
response = requests.get(geturl, headers=headers)
# 写入文本
self.show_text.insert(END, '获取到的链接:\n' + geturl +'\n下载中--{}{}'.format(name_a,info_a))
wget.download(geturl, '{}{}.mp4'.format(name_a,info_a))
return self.show_text.insert(END,'\n{} {}下载成功!'.format(name_a,info_a))
class Windows:
def __init__(self,tit_name,window_y_x):
self.douyin = Douyin()
self.douyin.window.title(tit_name)
self.douyin.window.geometry(window_y_x)
def Show_text(self,text,text_x,text_y,font=('华文行楷',16)):
self.douyin.tk.Label(self.douyin.window, text=text,font=font).place(x=text_x, y=text_y)
def Show_input_text(self,x,y):
self.douyin.show_text.place(x=x,y=y)
def Show_input(self,x,y):
self.douyin.tk.Entry(self.douyin.window, textvariable=self.douyin.var_token).place(x=x, y=y)
def Show_button(self,text,font,x,y):
self.douyin.tk.Button(self.douyin.window,text=text, font=font, command=self.douyin.get_tar).place(x=x, y=y)
def run(self):
self.douyin.window.mainloop()
def __del__(self):
return print('程序结束')
if __name__ == '__main__':
a=Douyin.Windows('抖音小视频','900x600')
a.Show_text('抖音无水印在下',50,50,font=("华文行楷",30))
a.Show_text('输入地址:',40,138,font=('华文行楷',20))
a.Show_input_text(300,150)
a.Show_input(160,150)
a.Show_button('go',20,150,200)
a.run()