[Python] 纯文本查看 复制代码
import wx
import re
import requests
import multiprocessing
from multiprocessing import Pool
req=requests.session()
def download_video(url, save_path):
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(save_path, 'wb') as file:
for chunk in response.iter_content(1024):
file.write(chunk)
t='下载成功'
else:
t='下载失败'
return t
def get_mid_string(html, start_str, end):
try:
start = html.find(start_str)
if start >= 0:
start += len(start_str)
end = html.find(end, start)
if end >= 0:
return html[start:end].strip()
except:
return None
def remove_special_characters(text):
pattern = r'[\\/:*?"<>|]'
cleaned_text = re.sub(pattern, '', text)
return cleaned_text
def keep_characters_before_hash(text):
parts = text.split('#', 1)
return parts[0]
def download_with_multiprocessing(url):
a=req.get('https://api.douyin.wtf/api?url={}'.format(url))
title = get_mid_string(a.text, '"desc":"', '"')
title1 = remove_special_characters(title)
title2 = keep_characters_before_hash(title1)
video_url = get_mid_string(a.text, 'nwm_video_url":"', '"')
save_path = f'./{title2}.mp4'
return download_video(video_url, save_path)
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='抖音下载', size=(400, 300), name='frame', style=541072960)
self.启动窗口 = wx.Panel(self)
self.Centre()
self.编辑框1 = wx.TextCtrl(self.启动窗口, size=(269, 221), pos=(7, 7), value='', name='text', style=1073741856)
self.标签1 = wx.StaticText(self.启动窗口, size=(80, 107), pos=(292, 16), label='默认保存与\n程序相同文\n件夹', name='staticText', style=2321)
self.标签1.SetForegroundColour((255, 0, 0, 255))
self.按钮1 = wx.Button(self.启动窗口, size=(80, 32), pos=(287, 215), label='按钮', name='button')
self.按钮1.Bind(wx.EVT_BUTTON, self.按钮1_按钮被单击)
self.编辑框2 = wx.TextCtrl(self.启动窗口, size=(98, 22), pos=(282, 134), value='', name='text', style=16)
def 按钮1_按钮被单击(self, event):
c=self.编辑框1.GetValue()
pattern = r'[a-zA-Z]+://[^"\'\s]*'
urls = re.findall(pattern,c)
with Pool(10) as pool:#如果报错修改为len(urls)
results = pool.map(download_with_multiprocessing, urls)
s = '\n'.join(results)
self.编辑框2.SetValue(s)
class myApp(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show(True)
return True
if __name__ == '__main__':
multiprocessing.freeze_support()#添加防止多进程打包后重启
app = myApp()
app.MainLoop()