banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

使用Python多進程下載抖音短視頻

废话少说,直接上货
抖音官網:https://www.douyin.com/

image

640_result

複製視頻鏈接地址

到文本框,然後點擊按鈕,按鈕上邊會提示下載成功還是下載失敗。

640 (1)

此時視頻文件就被下載成功。

640 (2)
完整代碼:


import wx
import re
import requests
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(len(urls)) as pool:
            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__':
    app = myApp()
    app.MainLoo
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。