优化页面初始化逻辑;继续完善下载功能

This commit is contained in:
g1879 2023-08-29 17:39:43 +08:00
parent b8e2be8799
commit 8170d53c97
10 changed files with 48 additions and 23 deletions

View File

@ -4,7 +4,6 @@
@Contact : g1879@qq.com @Contact : g1879@qq.com
""" """
from abc import abstractmethod from abc import abstractmethod
from pathlib import Path
from re import sub from re import sub
from urllib.parse import quote from urllib.parse import quote
@ -359,16 +358,15 @@ class DrissionElement(BaseElement):
class BasePage(BaseParser): class BasePage(BaseParser):
"""页面类的基类""" """页面类的基类"""
def __init__(self, timeout=None): def __init__(self):
"""初始化函数""" """初始化函数"""
self._url = None self._url = None
self.timeout = timeout if timeout is not None else 10 self._timeout = 10
self._url_available = None self._url_available = None
if not hasattr(self, 'retry_times'): self.retry_times = 3
self.retry_times = 3 self.retry_interval = 2
if not hasattr(self, 'retry_interval'):
self.retry_interval = 2
self._DownloadKit = None self._DownloadKit = None
self._download_path = ''
@property @property
def title(self): def title(self):
@ -399,7 +397,7 @@ class BasePage(BaseParser):
@property @property
def download_path(self): def download_path(self):
"""返回默认下载路径""" """返回默认下载路径"""
return str(Path(self._download_path).absolute()) return self._download_path
@property @property
def download(self): def download(self):

View File

@ -157,7 +157,7 @@ class DrissionElement(BaseElement):
class BasePage(BaseParser): class BasePage(BaseParser):
def __init__(self, timeout: float = None): def __init__(self):
self._url_available: bool = ... self._url_available: bool = ...
self.retry_times: int = ... self.retry_times: int = ...
self.retry_interval: float = ... self.retry_interval: float = ...

View File

@ -35,6 +35,7 @@ class ChromiumBase(BasePage):
:param tab_id: 要控制的标签页id不指定默认为激活的 :param tab_id: 要控制的标签页id不指定默认为激活的
:param timeout: 超时时间 :param timeout: 超时时间
""" """
super().__init__()
self._is_loading = None self._is_loading = None
self._root_id = None # object id self._root_id = None # object id
self._debug = False self._debug = False
@ -45,6 +46,7 @@ class ChromiumBase(BasePage):
self._listener = None self._listener = None
self._wait_download_flag = None self._wait_download_flag = None
self._download_rename = None self._download_rename = None
self._download_path = ''
if isinstance(address, int) or (isinstance(address, str) and address.isdigit()): if isinstance(address, int) or (isinstance(address, str) and address.isdigit()):
address = f'127.0.0.1:{address}' address = f'127.0.0.1:{address}'
@ -52,8 +54,8 @@ class ChromiumBase(BasePage):
self._set_start_options(address, None) self._set_start_options(address, None)
self._set_runtime_settings() self._set_runtime_settings()
self._connect_browser(tab_id) self._connect_browser(tab_id)
timeout = timeout if timeout is not None else self.timeouts.implicit if timeout is not None:
super().__init__(timeout) self.timeout = timeout
def _set_start_options(self, address, none): def _set_start_options(self, address, none):
"""设置浏览器启动属性 """设置浏览器启动属性

View File

@ -3,6 +3,7 @@
@Author : g1879 @Author : g1879
@Contact : g1879@qq.com @Contact : g1879@qq.com
""" """
from copy import copy
from re import search from re import search
from threading import Thread from threading import Thread
from time import sleep, perf_counter from time import sleep, perf_counter
@ -70,10 +71,13 @@ class ChromiumFrame(ChromiumBase):
attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs] attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs]
return f'<ChromiumFrame {self.frame_ele.tag} {" ".join(attrs)}>' return f'<ChromiumFrame {self.frame_ele.tag} {" ".join(attrs)}>'
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._page_load_strategy = self._target_page.page_load_strategy
self._download_path = self._target_page.download_path
def _driver_init(self, tab_id): def _driver_init(self, tab_id):
"""避免出现服务器500错误 """避免出现服务器500错误
@ -354,7 +358,7 @@ class ChromiumFrame(ChromiumBase):
@property @property
def download_path(self): def download_path(self):
return self.tab.download_path return self._download_path
def refresh(self): def refresh(self):
"""刷新frame页面""" """刷新frame页面"""

View File

@ -39,7 +39,7 @@ class ChromiumFrame(ChromiumBase):
def __repr__(self) -> str: ... def __repr__(self) -> str: ...
def _runtime_settings(self) -> None: ... def _set_runtime_settings(self) -> None: ...
def _driver_init(self, tab_id: str) -> None: ... def _driver_init(self, tab_id: str) -> None: ...

View File

@ -26,9 +26,10 @@ class ChromiumPage(ChromiumBase):
:param tab_id: 要控制的标签页id不指定默认为激活的 :param tab_id: 要控制的标签页id不指定默认为激活的
:param timeout: 超时时间 :param timeout: 超时时间
""" """
super().__init__(addr_driver_opts, tab_id, timeout) super().__init__(addr_driver_opts, tab_id)
self._page = self self._page = self
self._dl_mgr = BrowserDownloadManager(self) self._dl_mgr = BrowserDownloadManager(self)
self.set.timeouts(implicit=timeout)
def _set_start_options(self, addr_driver_opts, none): def _set_start_options(self, addr_driver_opts, none):
"""设置浏览器启动属性 """设置浏览器启动属性
@ -62,6 +63,8 @@ class ChromiumPage(ChromiumBase):
page_load=self._driver_options.timeouts['pageLoad'], page_load=self._driver_options.timeouts['pageLoad'],
script=self._driver_options.timeouts['script'], script=self._driver_options.timeouts['script'],
implicit=self._driver_options.timeouts['implicit']) 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._page_load_strategy = self._driver_options.page_load_strategy
self._download_path = self._driver_options.download_path self._download_path = self._driver_options.download_path
@ -460,9 +463,24 @@ class BrowserDownloadManager(object):
def _onDownloadWillBegin(self, **kwargs): def _onDownloadWillBegin(self, **kwargs):
"""用于获取弹出新标签页触发的下载任务""" """用于获取弹出新标签页触发的下载任务"""
sleep(.1) sleep(.2)
if kwargs['guid'] not in self._missions: 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): def _onDownloadProgress(self, **kwargs):
"""下载状态变化时执行""" """下载状态变化时执行"""

View File

@ -25,7 +25,7 @@ class ChromiumTab(ChromiumBase):
def _set_runtime_settings(self): 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_times = self.page.retry_times
self.retry_interval = self.page.retry_interval self.retry_interval = self.page.retry_interval
self._page_load_strategy = self.page.page_load_strategy self._page_load_strategy = self.page.page_load_strategy

View File

@ -28,6 +28,8 @@ class NetworkListener(object):
def stop(self) -> None: ... def stop(self) -> None: ...
def wait(self):...
@property @property
def results(self) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ... def results(self) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ...

View File

@ -26,14 +26,15 @@ class SessionPage(BasePage):
:param session_or_options: Session对象或SessionOptions对象 :param session_or_options: Session对象或SessionOptions对象
:param timeout: 连接超时时间为None时从ini文件读取 :param timeout: 连接超时时间为None时从ini文件读取
""" """
super().__init__()
self._response = None self._response = None
self._session = None self._session = None
self._set = None self._set = None
self._set_start_options(session_or_options, None) self._set_start_options(session_or_options, None)
self._set_runtime_settings() self._set_runtime_settings()
self._create_session() self._create_session()
timeout = timeout if timeout is not None else self.timeout if timeout is not None:
super().__init__(timeout) self.timeout = timeout
def _set_start_options(self, session_or_options, none): def _set_start_options(self, session_or_options, none):
"""启动配置 """启动配置

View File

@ -122,7 +122,7 @@ class ChromiumBaseSetter(object):
:param path: 下载路径 :param path: 下载路径
:return: None :return: None
""" """
self._page._download_path = str(path) self._page._download_path = str(Path(path).absolute())
if self._page._DownloadKit: if self._page._DownloadKit:
self._page._DownloadKit.set.goal_path(path) self._page._DownloadKit.set.goal_path(path)
@ -193,7 +193,7 @@ class SessionPageSetter(object):
:param path: 下载路径 :param path: 下载路径
:return: None :return: None
""" """
self._page._download_path = str(path) self._page._download_path = str(Path(path).absolute())
if self._page._DownloadKit: if self._page._DownloadKit:
self._page._DownloadKit.set.goal_path(path) self._page._DownloadKit.set.goal_path(path)