diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 6de9f07..7d17ec5 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] -__version__ = '4.0.4' +__version__ = '4.0.4.1' diff --git a/DrissionPage/_base/driver.py b/DrissionPage/_base/driver.py index b442109..293f3d0 100644 --- a/DrissionPage/_base/driver.py +++ b/DrissionPage/_base/driver.py @@ -14,6 +14,7 @@ from requests import get from websocket import (WebSocketTimeoutException, WebSocketConnectionClosedException, create_connection, WebSocketException, WebSocketBadStatusException) +from .._functions.settings import Settings from ..errors import PageDisconnectedError, TargetNotFoundError @@ -186,7 +187,7 @@ class Driver(object): if self._stopped.is_set(): return {'error': 'connection disconnected', 'type': 'connection_error'} - timeout = kwargs.pop('_timeout', 30) + timeout = kwargs.pop('_timeout', Settings.cdp_timeout) result = self._send({'method': _method, 'params': kwargs}, timeout=timeout) if 'result' not in result and 'error' in result: kwargs['_timeout'] = timeout diff --git a/DrissionPage/_configs/chromium_options.py b/DrissionPage/_configs/chromium_options.py index c5054af..8dbdf1e 100644 --- a/DrissionPage/_configs/chromium_options.py +++ b/DrissionPage/_configs/chromium_options.py @@ -23,67 +23,56 @@ class ChromiumOptions(object): self.clear_file_flags = False self._headless = None - if read_file is not False: - ini_path = str(ini_path) if ini_path else None - om = OptionsManager(ini_path) - self.ini_path = str(Path(om.ini_path).absolute()) + if read_file is False: + ini_path = False + self.ini_path = None + elif ini_path: + ini_path = Path(ini_path).absolute() + if not ini_path.exists(): + raise ValueError(f'文件不存在:{ini_path}') + self.ini_path = str(ini_path) + else: + self.ini_path = str(Path(__file__).parent / 'configs.ini') + om = OptionsManager(ini_path) - options = om.chromium_options - self._download_path = om.paths.get('download_path', None) or None - self._tmp_path = om.paths.get('tmp_path', None) or None - self._arguments = options.get('arguments', []) - self._browser_path = options.get('browser_path', '') - self._extensions = options.get('extensions', []) - self._prefs = options.get('prefs', {}) - self._flags = options.get('flags', {}) - self._address = options.get('address', None) - self._load_mode = options.get('load_mode', 'normal') - self._system_user_path = options.get('system_user_path', False) - self._existing_only = options.get('existing_only', False) + options = om.chromium_options + self._download_path = om.paths.get('download_path', None) or None + self._tmp_path = om.paths.get('tmp_path', None) or None + self._arguments = options.get('arguments', []) + self._browser_path = options.get('browser_path', '') + self._extensions = options.get('extensions', []) + self._prefs = options.get('prefs', {}) + self._flags = options.get('flags', {}) + self._address = options.get('address', None) + self._load_mode = options.get('load_mode', 'normal') + self._system_user_path = options.get('system_user_path', False) + self._existing_only = options.get('existing_only', False) - self._proxy = om.proxies.get('http', None) or om.proxies.get('https', None) + self._proxy = om.proxies.get('http', None) or om.proxies.get('https', None) - user_path = user = False - for arg in self._arguments: - if arg.startswith('--user-data-dir='): - self.set_paths(user_data_path=arg[16:]) - user_path = True - if arg.startswith('--profile-directory='): - self.set_user(arg[20:]) - user = True - if user and user_path: - break + user_path = user = False + for arg in self._arguments: + if arg.startswith('--user-data-dir='): + self.set_paths(user_data_path=arg[16:]) + user_path = True + if arg.startswith('--profile-directory='): + self.set_user(arg[20:]) + user = True + if user and user_path: + break - timeouts = om.timeouts - self._timeouts = {'base': timeouts['base'], - 'page_load': timeouts['page_load'], - 'script': timeouts['script']} + timeouts = om.timeouts + self._timeouts = {'base': timeouts['base'], + 'page_load': timeouts['page_load'], + 'script': timeouts['script']} - self._auto_port = options.get('auto_port', False) + self._auto_port = options.get('auto_port', False) - others = om.others - self._retry_times = others.get('retry_times', 3) - self._retry_interval = others.get('retry_interval', 2) + others = om.others + self._retry_times = others.get('retry_times', 3) + self._retry_interval = others.get('retry_interval', 2) - return - - self.ini_path = None - self._browser_path = "chrome" - self._arguments = [] - self._download_path = None - self._tmp_path = None - self._extensions = [] - self._prefs = {} - self._flags = {} - self._timeouts = {'base': 10, 'page_load': 30, 'script': 30} - self._address = '127.0.0.1:9222' - self._load_mode = 'normal' - self._proxy = None - self._auto_port = False - self._system_user_path = False - self._existing_only = False - self._retry_times = 3 - self._retry_interval = 2 + return @property def download_path(self): @@ -538,8 +527,8 @@ class ChromiumOptions(object): for i in attrs: om.set_item('chromium_options', i, self.__getattribute__(f'_{i}')) # 设置代理 - om.set_item('proxies', 'http', self._proxy) - om.set_item('proxies', 'https', self._proxy) + om.set_item('proxies', 'http', self._proxy or '') + om.set_item('proxies', 'https', self._proxy or '') # 设置路径 om.set_item('paths', 'download_path', self._download_path or '') om.set_item('paths', 'tmp_path', self._tmp_path or '') diff --git a/DrissionPage/_configs/options_manage.py b/DrissionPage/_configs/options_manage.py index bb5d964..e6867e3 100644 --- a/DrissionPage/_configs/options_manage.py +++ b/DrissionPage/_configs/options_manage.py @@ -8,7 +8,6 @@ from configparser import RawConfigParser, NoSectionError, NoOptionError from pathlib import Path from pprint import pprint -from time import sleep class OptionsManager(object): @@ -18,22 +17,63 @@ class OptionsManager(object): """初始化,读取配置文件,如没有设置临时文件夹,则设置并新建 :param path: ini文件的路径,为None则找项目文件夹下的,找不到则读取模块文件夹下的 """ - if path is None: - if Path('dp_configs.ini').exists(): - self.ini_path = 'dp_configs.ini' - else: - self.ini_path = str(Path(__file__).parent / 'configs.ini') - elif path == 'default': - self.ini_path = str(Path(__file__).parent / 'configs.ini') + if path is False: + self.ini_path = None else: - self.ini_path = str(path) + default_configs = Path(__file__).parent / 'configs.ini' + if path is None: + dp_configs = Path('dp_configs.ini') + if dp_configs.exists(): + self.ini_path = dp_configs + else: + self.ini_path = default_configs + elif path == 'default': + self.ini_path = default_configs + else: + self.ini_path = Path(path) - if not Path(self.ini_path).exists(): - print('\nini文件不存在。\n如果是打包使用,请查看打包注意事项\n' - 'https://g1879.gitee.io/drissionpagedocs/advance/packaging/') - sleep(10) self._conf = RawConfigParser() - self._conf.read(self.ini_path, encoding='utf-8') + if path is not False and self.ini_path.exists(): + self.file_exists = True + self._conf.read(self.ini_path, encoding='utf-8') + else: + self.file_exists = False + self._conf.add_section('paths') + self._conf.add_section('chromium_options') + self._conf.add_section('session_options') + self._conf.add_section('timeouts') + self._conf.add_section('proxies') + self._conf.add_section('others') + self.set_item('paths', 'download_path', '') + self.set_item('paths', 'tmp_path', '') + self.set_item('chromium_options', 'address', '127.0.0.1:9222') + self.set_item('chromium_options', 'browser_path', 'chrome') + self.set_item('chromium_options', 'arguments', "['--no-default-browser-check', '--disable-suggestions-ui', " + "'--no-first-run', '--disable-infobars', " + "'--disable-popup-blocking', '--hide-crash-restore-bubble', " + "'--disable-features=PrivacySandboxSettings4']") + self.set_item('chromium_options', 'extensions', '[]') + self.set_item('chromium_options', 'prefs', "{'profile.default_content_settings.popups': 0, " + "'profile.default_content_setting_values': " + "{'notifications': 2}}") + self.set_item('chromium_options', 'flags', '{}') + self.set_item('chromium_options', 'load_mode', 'normal') + self.set_item('chromium_options', 'user', 'Default') + self.set_item('chromium_options', 'auto_port', 'False') + self.set_item('chromium_options', 'system_user_path', 'False') + self.set_item('chromium_options', 'existing_only', 'False') + self.set_item('session_options', 'headers', "{'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X " + "10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10." + "1.2 Safari/603.3.8', 'accept': 'text/html,application/xhtml" + "+xml,application/xml;q=0.9,*/*;q=0.8', 'connection': " + "'keep-alive', 'accept-charset': 'GB2312,utf-8;q=0.7,*;q=0.7'}") + self.set_item('timeouts', 'base', '10') + self.set_item('timeouts', 'page_load', '30') + self.set_item('timeouts', 'script', '30') + self.set_item('proxies', 'http', '') + self.set_item('proxies', 'https', '') + self.set_item('others', 'retry_times', '3') + self.set_item('others', 'retry_interval', '2') def __getattr__(self, item): """以dict形似返回获取大项信息 @@ -100,7 +140,9 @@ class OptionsManager(object): if path == 'default': path = default_path elif path is None: - path = Path(self.ini_path).absolute() + if self.ini_path is None: + raise ValueError('ini_path未设置。') + path = self.ini_path.absolute() else: path = Path(path).absolute() @@ -113,6 +155,7 @@ class OptionsManager(object): if path == str(default_path): print('以后程序可自动从文件加载配置。') + self.file_exists = True return path def save_to_default(self): diff --git a/DrissionPage/_configs/options_manage.pyi b/DrissionPage/_configs/options_manage.pyi index 6bb3e4f..80e438d 100644 --- a/DrissionPage/_configs/options_manage.pyi +++ b/DrissionPage/_configs/options_manage.pyi @@ -6,11 +6,13 @@ @License : BSD 3-Clause. """ from configparser import RawConfigParser -from typing import Any +from pathlib import Path +from typing import Any, Optional class OptionsManager(object): - ini_path: str = ... + ini_path: Optional[Path] = ... + file_exists: bool = ... _conf: RawConfigParser = ... def __init__(self, path: str = None): ... diff --git a/DrissionPage/_configs/session_options.py b/DrissionPage/_configs/session_options.py index 135210a..1983801 100644 --- a/DrissionPage/_configs/session_options.py +++ b/DrissionPage/_configs/session_options.py @@ -27,6 +27,18 @@ class SessionOptions(object): self._timeout = 10 self._del_set = set() # 记录要从ini文件删除的参数 + if read_file is False: + ini_path = False + self.ini_path = None + elif ini_path: + ini_path = Path(ini_path).absolute() + if not ini_path.exists(): + raise ValueError(f'文件不存在:{ini_path}') + self.ini_path = str(ini_path) + else: + self.ini_path = str(Path(__file__).parent / 'configs.ini') + om = OptionsManager(ini_path) + self._headers = None self._cookies = None self._auth = None @@ -39,15 +51,6 @@ class SessionOptions(object): self._stream = None self._trust_env = None self._max_redirects = None - self._retry_times = 3 - self._retry_interval = 2 - - if read_file is False: - return - - ini_path = str(ini_path) if ini_path else None - om = OptionsManager(ini_path) - self.ini_path = om.ini_path options = om.session_options if options.get('headers', None) is not None: @@ -381,8 +384,8 @@ class SessionOptions(object): om.set_item('paths', 'download_path', self.download_path or '') om.set_item('timeouts', 'base', self.timeout) - om.set_item('proxies', 'http', self.proxies.get('http', None)) - om.set_item('proxies', 'https', self.proxies.get('https', None)) + om.set_item('proxies', 'http', self.proxies.get('http', '')) + om.set_item('proxies', 'https', self.proxies.get('https', '')) om.set_item('others', 'retry_times', self.retry_times) om.set_item('others', 'retry_interval', self.retry_interval) diff --git a/DrissionPage/_functions/browser.py b/DrissionPage/_functions/browser.py index ad8803e..4f79269 100644 --- a/DrissionPage/_functions/browser.py +++ b/DrissionPage/_functions/browser.py @@ -285,7 +285,7 @@ def _remove_arg_from_dict(target_dict: dict, arg: str) -> None: def get_chrome_path(ini_path): """从ini文件或系统变量中获取chrome可执行文件的路径""" # -----------从ini文件中获取-------------- - if ini_path: + if ini_path and Path(ini_path).exists(): path = OptionsManager(ini_path).chromium_options.get('browser_path', None) if path and Path(path).is_file(): return str(path) diff --git a/DrissionPage/_functions/settings.py b/DrissionPage/_functions/settings.py index 07374ac..8f57669 100644 --- a/DrissionPage/_functions/settings.py +++ b/DrissionPage/_functions/settings.py @@ -12,3 +12,4 @@ class Settings(object): raise_when_click_failed = False raise_when_wait_failed = False singleton_tab_obj = True + cdp_timeout = 30 diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 48301c3..dd0780c 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -627,7 +627,10 @@ class WindowSetter(object): :param bounds: 控制数据 :return: None """ - self._page.run_cdp('Browser.setWindowBounds', windowId=self._window_id, bounds=bounds) + try: + self._page.run_cdp('Browser.setWindowBounds', windowId=self._window_id, bounds=bounds) + except: + raise RuntimeError('浏览器全屏或最小化状态时请先调用set.window.normal()恢复正常状态。') # ------------即将废除----------