diff --git a/DrissionPage/base.py b/DrissionPage/base.py index 33aa5c1..c7b89d8 100644 --- a/DrissionPage/base.py +++ b/DrissionPage/base.py @@ -4,7 +4,6 @@ @Contact : g1879@qq.com """ from abc import abstractmethod -from pathlib import Path from re import sub from urllib.parse import quote @@ -359,16 +358,15 @@ class DrissionElement(BaseElement): class BasePage(BaseParser): """页面类的基类""" - def __init__(self, timeout=None): + def __init__(self): """初始化函数""" self._url = None - self.timeout = timeout if timeout is not None else 10 + self._timeout = 10 self._url_available = None - if not hasattr(self, 'retry_times'): - self.retry_times = 3 - if not hasattr(self, 'retry_interval'): - self.retry_interval = 2 + self.retry_times = 3 + self.retry_interval = 2 self._DownloadKit = None + self._download_path = '' @property def title(self): @@ -399,7 +397,7 @@ class BasePage(BaseParser): @property def download_path(self): """返回默认下载路径""" - return str(Path(self._download_path).absolute()) + return self._download_path @property def download(self): diff --git a/DrissionPage/base.pyi b/DrissionPage/base.pyi index d24d305..6c49719 100644 --- a/DrissionPage/base.pyi +++ b/DrissionPage/base.pyi @@ -157,7 +157,7 @@ class DrissionElement(BaseElement): class BasePage(BaseParser): - def __init__(self, timeout: float = None): + def __init__(self): self._url_available: bool = ... self.retry_times: int = ... self.retry_interval: float = ... diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 8c18e5e..0264207 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -35,6 +35,7 @@ class ChromiumBase(BasePage): :param tab_id: 要控制的标签页id,不指定默认为激活的 :param timeout: 超时时间 """ + super().__init__() self._is_loading = None self._root_id = None # object id self._debug = False @@ -45,6 +46,7 @@ class ChromiumBase(BasePage): self._listener = None self._wait_download_flag = None self._download_rename = None + self._download_path = '' if isinstance(address, int) or (isinstance(address, str) and address.isdigit()): address = f'127.0.0.1:{address}' @@ -52,8 +54,8 @@ class ChromiumBase(BasePage): self._set_start_options(address, None) self._set_runtime_settings() self._connect_browser(tab_id) - timeout = timeout if timeout is not None else self.timeouts.implicit - super().__init__(timeout) + if timeout is not None: + self.timeout = timeout def _set_start_options(self, address, none): """设置浏览器启动属性 diff --git a/DrissionPage/chromium_frame.py b/DrissionPage/chromium_frame.py index 001d49f..9bd709d 100644 --- a/DrissionPage/chromium_frame.py +++ b/DrissionPage/chromium_frame.py @@ -3,6 +3,7 @@ @Author : g1879 @Contact : g1879@qq.com """ +from copy import copy from re import search from threading import Thread from time import sleep, perf_counter @@ -70,10 +71,13 @@ class ChromiumFrame(ChromiumBase): attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs] return f'' - def _runtime_settings(self): + def _set_runtime_settings(self): """重写设置浏览器运行参数方法""" - self._timeouts = self._target_page.timeouts + self._timeouts = copy(self._target_page.timeouts) + self.retry_times = self._target_page.retry_times + self.retry_interval = self._target_page.retry_interval self._page_load_strategy = self._target_page.page_load_strategy + self._download_path = self._target_page.download_path def _driver_init(self, tab_id): """避免出现服务器500错误 @@ -354,7 +358,7 @@ class ChromiumFrame(ChromiumBase): @property def download_path(self): - return self.tab.download_path + return self._download_path def refresh(self): """刷新frame页面""" diff --git a/DrissionPage/chromium_frame.pyi b/DrissionPage/chromium_frame.pyi index 1a798f6..e310dc6 100644 --- a/DrissionPage/chromium_frame.pyi +++ b/DrissionPage/chromium_frame.pyi @@ -39,7 +39,7 @@ class ChromiumFrame(ChromiumBase): def __repr__(self) -> str: ... - def _runtime_settings(self) -> None: ... + def _set_runtime_settings(self) -> None: ... def _driver_init(self, tab_id: str) -> None: ... diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 14bbfbe..db71079 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -26,9 +26,10 @@ class ChromiumPage(ChromiumBase): :param tab_id: 要控制的标签页id,不指定默认为激活的 :param timeout: 超时时间 """ - super().__init__(addr_driver_opts, tab_id, timeout) + super().__init__(addr_driver_opts, tab_id) self._page = self self._dl_mgr = BrowserDownloadManager(self) + self.set.timeouts(implicit=timeout) def _set_start_options(self, addr_driver_opts, none): """设置浏览器启动属性 @@ -62,6 +63,8 @@ class ChromiumPage(ChromiumBase): page_load=self._driver_options.timeouts['pageLoad'], script=self._driver_options.timeouts['script'], implicit=self._driver_options.timeouts['implicit']) + if self._driver_options.timeouts['implicit'] is not None: + self._timeout = self._driver_options.timeouts['implicit'] self._page_load_strategy = self._driver_options.page_load_strategy self._download_path = self._driver_options.download_path @@ -460,9 +463,24 @@ class BrowserDownloadManager(object): def _onDownloadWillBegin(self, **kwargs): """用于获取弹出新标签页触发的下载任务""" - sleep(.1) + sleep(.2) if kwargs['guid'] not in self._missions: - self.add_mission(kwargs['guid'], self._page.download_path, kwargs['suggestedFilename']) + if self._page._wait_download_flag is False: + self._page.run_cdp('Browser.cancelDownload', guid=kwargs['guid']) + + if self._page._download_rename: + tmp = kwargs['suggestedFilename'].rsplit('.', 1) + ext_name = tmp[-1] if len(tmp) > 1 else '' + tmp = self._page._download_rename.rsplit('.', 1) + ext_rename = tmp[-1] if len(tmp) > 1 else '' + n = self._page._download_rename if ext_rename == ext_name else f'{self._page._download_rename}.{ext_name}' + self._download_rename = None + + else: + n = kwargs['suggestedFilename'] + + self._page._dl_mgr.add_mission(kwargs['guid'], self._page.download_path, n) + self._wait_download_flag = {'url': kwargs['url'], 'name': n} def _onDownloadProgress(self, **kwargs): """下载状态变化时执行""" diff --git a/DrissionPage/chromium_tab.py b/DrissionPage/chromium_tab.py index 9f68d5f..a46550b 100644 --- a/DrissionPage/chromium_tab.py +++ b/DrissionPage/chromium_tab.py @@ -25,7 +25,7 @@ class ChromiumTab(ChromiumBase): def _set_runtime_settings(self): """重写设置浏览器运行参数方法""" - self._timeouts = self.page.timeouts + self._timeouts = copy(self.page.timeouts) self.retry_times = self.page.retry_times self.retry_interval = self.page.retry_interval self._page_load_strategy = self.page.page_load_strategy diff --git a/DrissionPage/network_listener.pyi b/DrissionPage/network_listener.pyi index 759f7b2..76c5e72 100644 --- a/DrissionPage/network_listener.pyi +++ b/DrissionPage/network_listener.pyi @@ -28,6 +28,8 @@ class NetworkListener(object): def stop(self) -> None: ... + def wait(self):... + @property def results(self) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ... diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 7e07aa7..0931de5 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -26,14 +26,15 @@ class SessionPage(BasePage): :param session_or_options: Session对象或SessionOptions对象 :param timeout: 连接超时时间,为None时从ini文件读取 """ + super().__init__() self._response = None self._session = None self._set = None self._set_start_options(session_or_options, None) self._set_runtime_settings() self._create_session() - timeout = timeout if timeout is not None else self.timeout - super().__init__(timeout) + if timeout is not None: + self.timeout = timeout def _set_start_options(self, session_or_options, none): """启动配置 diff --git a/DrissionPage/setter.py b/DrissionPage/setter.py index dcfd210..6972713 100644 --- a/DrissionPage/setter.py +++ b/DrissionPage/setter.py @@ -122,7 +122,7 @@ class ChromiumBaseSetter(object): :param path: 下载路径 :return: None """ - self._page._download_path = str(path) + self._page._download_path = str(Path(path).absolute()) if self._page._DownloadKit: self._page._DownloadKit.set.goal_path(path) @@ -193,7 +193,7 @@ class SessionPageSetter(object): :param path: 下载路径 :return: None """ - self._page._download_path = str(path) + self._page._download_path = str(Path(path).absolute()) if self._page._DownloadKit: self._page._DownloadKit.set.goal_path(path)