Python 多线程下载

Python 多线程下载,python,google-chrome,selenium,Python,Google Chrome,Selenium,由于我位于中国,使用YouTube对我来说是一件头疼的事。我尝试构建一个播放列表下载程序(我知道youtube dl,但想学习smth new),现在它基本上完成了它的工作。 由于连接不稳定(中国的vpn),如果出现连接问题,浏览器将停止下载,但不会将控制权返回到我的脚本。如何处理此异常? 整个档案都在这里 以下是代码(从3d party服务下载): 如果我正确理解您的问题,您的下载将完全停止,而不会将控制权返回到脚本,您需要一种方法来检测/处理这种情况吗 您可以尝试混合以下内容和软件包: fr

由于我位于中国,使用YouTube对我来说是一件头疼的事。我尝试构建一个播放列表下载程序(我知道youtube dl,但想学习smth new),现在它基本上完成了它的工作。 由于连接不稳定(中国的vpn),如果出现连接问题,浏览器将停止下载,但不会将控制权返回到我的脚本。如何处理此异常? 整个档案都在这里 以下是代码(从3d party服务下载):


如果我正确理解您的问题,您的下载将完全停止,而不会将控制权返回到脚本,您需要一种方法来检测/处理这种情况吗

您可以尝试混合以下内容和软件包:

from retry import retry
from timeout_decorator import timeout, TimeoutError

# <...>

@retry(TimeoutError, tries=3)
@timeout(300)  # Wait 300 seconds before raising the timeout exception
def _download(chromedriver, savemediaurl, entry, index):
    driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
    try:
        saved_file = downloadFromSavemedia(savemediaurl, driver, entry[1], index+1)
        if saved_file:
            old_name = entry[0] + '.mp4'
            checkFileStatus(old_name)
            new_name = str(index+1).zfill(2) + '. ' + entry[0] + '.mp4'
            os.rename(old_name, new_name)
    finally:
        driver.quit()

def getDownloads(clip_links, home_dir):
    """Manages the whole downloading process
    - opens webdrive with Chrome
    - saves and renames files from valid links
    -  quits webdrive"""
    chromeOptions = webdriver.ChromeOptions()
    prefs = {"download.default_directory": home_dir}
    chromeOptions.add_experimental_option("prefs", prefs)
    chromedriver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'
    savemediaurl = 'http://savemedia.com/'
    for index, entry in enumerate(clip_links):
        _download(chromedriver, savemediaurl, entry, index)

    return
从重试导入重试
从timeout\u decorator导入timeout,TimeoutError
# 
@重试(TimeoutError,trys=3)
@超时(300)#在引发超时异常之前等待300秒
def_下载(chromedriver、savemediaurl、条目、索引):
driver=webdriver.Chrome(可执行文件路径=chromedriver,Chrome\u选项=chromeOptions)
尝试:
saved_file=从SaveMedia下载(savemediaurl,驱动程序,条目[1],索引+1)
如果保存了\u文件:
旧名称=条目[0]+'.mp4'
checkFileStatus(旧名称)
new_name=str(索引+1).zfill(2)+'.+条目[0]+'.mp4'
重命名(旧名称、新名称)
最后:
driver.quit()
def getDownloads(剪辑链接、主页目录):
“”“管理整个下载过程
-使用Chrome打开webdrive
-保存和重命名有效链接中的文件
-退出webdrive“”
chromeOptions=webdriver.chromeOptions()
prefs={“download.default_目录”:home_dir}
chromeOptions.添加实验选项(“prefs”,prefs)
chromedriver='C:\ProgramFiles(x86)\Google\Chrome\Application\chromedriver.exe'
savemediaurl=http://savemedia.com/'
对于索引,枚举中的条目(剪辑链接):
_下载(chromedriver、savemediaurl、条目、索引)
返回
在上面的例子中,如果整个函数调用花费的时间超过5分钟,超时装饰器将抛出TimeoutError异常,然后重试装饰器将捕获并处理该异常。这将重复最多3次,之后重试装饰程序将重新引发TimeoutError

有两点需要注意:

首先,您需要调整超时值。下载可能仍在进行,但所花的时间比预期的要长,并且超时装饰程序仍会终止下载

其次,我添加了try/finally块,以确保在抛出超时时驱动程序正常关闭。如果没有这一点,你将开始收集僵尸铬进程,这将使你的系统陷入困境

(这些都未经测试)

from retry import retry
from timeout_decorator import timeout, TimeoutError

# <...>

@retry(TimeoutError, tries=3)
@timeout(300)  # Wait 300 seconds before raising the timeout exception
def _download(chromedriver, savemediaurl, entry, index):
    driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
    try:
        saved_file = downloadFromSavemedia(savemediaurl, driver, entry[1], index+1)
        if saved_file:
            old_name = entry[0] + '.mp4'
            checkFileStatus(old_name)
            new_name = str(index+1).zfill(2) + '. ' + entry[0] + '.mp4'
            os.rename(old_name, new_name)
    finally:
        driver.quit()

def getDownloads(clip_links, home_dir):
    """Manages the whole downloading process
    - opens webdrive with Chrome
    - saves and renames files from valid links
    -  quits webdrive"""
    chromeOptions = webdriver.ChromeOptions()
    prefs = {"download.default_directory": home_dir}
    chromeOptions.add_experimental_option("prefs", prefs)
    chromedriver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'
    savemediaurl = 'http://savemedia.com/'
    for index, entry in enumerate(clip_links):
        _download(chromedriver, savemediaurl, entry, index)

    return