From 3b09e69d43f33e9b09266169f7995b4c703cbc13 Mon Sep 17 00:00:00 2001 From: g1879 Date: Sat, 21 Jan 2023 22:52:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E4=BD=BF=E7=94=A8downloadKit=E4=B8=8B=E8=BD=BD=E6=97=B6?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E8=A2=AB=E5=BC=BA=E5=88=B6=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_page.py | 19 +++++++++++++------ DrissionPage/chromium_page.pyi | 8 ++++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index b929004..25fbe06 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -5,6 +5,7 @@ """ from pathlib import Path from platform import system +from threading import Thread from time import perf_counter, sleep from DownloadKit import DownloadKit @@ -402,6 +403,7 @@ class ChromiumDownloadSetter(DownloadSetter): def __init__(self, page): super().__init__(page) self._behavior = 'allow' + self._download_th = None def save_path(self, path): """设置下载路径 @@ -436,12 +438,17 @@ class ChromiumDownloadSetter(DownloadSetter): self._behavior = 'deny' def _download_by_DownloadKit(self, **kwargs): - gid = kwargs['guid'] - self._page.run_cdp('Browser.cancelDownload', guid=gid, not_change=True) - url = kwargs['url'] - name = kwargs['suggestedFilename'] - print(f'下载:{url}') - self._page.download.add(url, goal_path=self._page.download_path, rename=name) + """拦截浏览器下载并用downloadKit下载""" + if self._download_th is None or not self._download_th.is_alive(): + self._download_th = Thread(target=self._wait_download_complete, daemon=False) + self._download_th.start() + self._page.download.add(kwargs['url'], self._page.download_path, kwargs['suggestedFilename']) + self._page.run_cdp('Browser.cancelDownload', guid=kwargs['guid'], not_change=True) + print(f'下载:{kwargs["url"]}') + + def _wait_download_complete(self): + """等待下载完成""" + self._page.download.wait() class Alert(object): diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi index 8712593..a15dbe4 100644 --- a/DrissionPage/chromium_page.pyi +++ b/DrissionPage/chromium_page.pyi @@ -5,6 +5,7 @@ """ from os import popen from pathlib import Path +from threading import Thread from typing import Union, Tuple, List from DownloadKit import DownloadKit @@ -37,12 +38,12 @@ class ChromiumPage(ChromiumBase): addr_driver_opts: Union[str, ChromiumDriver, DriverOptions] = None, tab_id: str = None) -> None: ... - def _init_page(self, tab_id: str = None) -> None: ... - def _set_options(self) -> None: ... def _set_chromium_options(self) -> None: ... + def _init_page(self, tab_id: str = None) -> None: ... + @property def tabs_count(self) -> int: ... @@ -108,6 +109,9 @@ class ChromiumDownloadSetter(object): def __init__(self, page: ChromiumPage): self._page: ChromiumPage = ... self._behavior: str = ... + self._download_th: Thread = ... + + def _wait_download_complete(self) -> None: ... def save_path(self, path) -> None: ...