diff --git a/DrissionPage/config.py b/DrissionPage/config.py index 84c2b19..2e07abf 100644 --- a/DrissionPage/config.py +++ b/DrissionPage/config.py @@ -1,6 +1,6 @@ # -*- coding:utf-8 -*- """ -配置文件 +管理配置的类 @Author : g1879 @Contact : g1879@qq.com @File : config.py @@ -28,10 +28,10 @@ class OptionsManager(object): self._chrome_options = None self._session_options = None - if 'global_tmp_path' not in self.paths or not self.get_value('paths', 'global_tmp_path'): - global_tmp_path = str((Path(__file__).parent / 'tmp').absolute()) - Path(global_tmp_path).mkdir(parents=True, exist_ok=True) - self.set_item('paths', 'global_tmp_path', global_tmp_path) + if 'tmp_path' not in self.paths or not self.get_value('paths', 'tmp_path'): + tmp_path = str((Path(__file__).parent / 'tmp').absolute()) + Path(tmp_path).mkdir(parents=True, exist_ok=True) + self.set_item('paths', 'tmp_path', tmp_path) self.save(self.ini_path) def __text__(self) -> str: @@ -128,9 +128,9 @@ class OptionsManager(object): class SessionOptions(object): def __init__(self, read_file: bool = True, ini_path: str = None): - """ - :param read_file: - :param ini_path: + """requests的Session对象配置类 \n + :param read_file: 是否从文件读取配置 + :param ini_path: ini文件路径 """ self.ini_path = None self._headers = None @@ -187,115 +187,168 @@ class SessionOptions(object): if options_dict.get('max_redirects', None) is not None: self._headers = options_dict['max_redirects'] - # self._cookies = options_dict.get('cookies', None) - # self._auth = options_dict.get('auth', None) - # self._proxies = options_dict.get('proxies', None) - # self._hooks = options_dict.get('hooks', None) - # self._params = options_dict.get('params', None) - # self._verify = options_dict.get('verify', True) - # self._cert = options_dict.get('cert', None) - # self._adapters = options_dict.get('adapters', None) - # self._stream = options_dict.get('stream', None) - # self._trust_env = options_dict.get('trust_env', True) - # self._max_redirects = options_dict.get('max_redirects', None) - @property def headers(self) -> dict: + """返回headers设置信息""" return self._headers @property def cookies(self) -> dict: + """返回cookies设置信息""" return self._cookies @property def auth(self) -> tuple: + """返回auth设置信息""" return self._auth @property def proxies(self) -> dict: + """返回proxies设置信息""" return self._proxies @property def hooks(self) -> dict: + """返回hooks设置信息""" return self._hooks @property def params(self) -> dict: + """返回params设置信息""" return self._params @property def verify(self) -> bool: + """返回verify设置信息""" return self._verify @property def cert(self) -> Union[str, tuple]: + """返回cert设置信息""" return self._cert @property def adapters(self): + """返回adapters设置信息""" return self._adapters @property def stream(self) -> bool: + """返回stream设置信息""" return self._stream @property def trust_env(self) -> bool: + """返回trust_env设置信息""" return self._trust_env @property def max_redirects(self) -> int: + """返回max_redirects设置信息""" return self._max_redirects @headers.setter def headers(self, headers: dict) -> None: + """设置headers参数 \n + :param headers: 参数值 + :return: None + """ self._headers = {key.lower(): headers[key] for key in headers} @cookies.setter def cookies(self, cookies: dict) -> None: + """设置cookies参数 \n + :param cookies: 参数值 + :return: None + """ self._cookies = cookies @auth.setter def auth(self, auth: tuple) -> None: + """设置auth参数 \n + :param auth: 参数值 + :return: None + """ self._auth = auth @proxies.setter def proxies(self, proxies: dict) -> None: + """设置proxies参数 \n + :param proxies: 参数值 + :return: None + """ self._proxies = proxies @hooks.setter def hooks(self, hooks: dict) -> None: + """设置hooks参数 \n + :param hooks: 参数值 + :return: None + """ self._hooks = hooks @params.setter def params(self, params: dict) -> None: + """设置params参数 \n + :param params: 参数值 + :return: None + """ self._params = params @verify.setter def verify(self, verify: bool) -> None: + """设置verify参数 \n + :param verify: 参数值 + :return: None + """ self._verify = verify @cert.setter def cert(self, cert: Union[str, tuple]) -> None: + """设置cert参数 \n + :param cert: 参数值 + :return: None + """ self._cert = cert @adapters.setter def adapters(self, adapters) -> None: + """设置 \n + :param adapters: 参数值 + :return: None + """ self._adapters = adapters @stream.setter def stream(self, stream: bool) -> None: + """设置stream参数 \n + :param stream: 参数值 + :return: None + """ self._stream = stream @trust_env.setter def trust_env(self, trust_env: bool) -> None: + """设置trust_env参数 \n + :param trust_env: 参数值 + :return: None + """ self._trust_env = trust_env @max_redirects.setter def max_redirects(self, max_redirects: int) -> None: + """设置max_redirects参数 \n + :param max_redirects: 参数值 + :return: None + """ self._max_redirects = max_redirects def set_header(self, attr: str, value: str): + """设置header中一个项 \n + :param attr: 设置名称 + :param value: 设置值 + :return: 返回当前对象 + """ if self._headers is None: self._headers = {} @@ -303,6 +356,10 @@ class SessionOptions(object): return self def remove_header(self, attr: str): + """从headers中删除一个设置 \n + :param attr: 要删除的设置 + :return: 返回当前对象 + """ if self._headers is None: return self @@ -315,7 +372,7 @@ class SessionOptions(object): def save(self, path: str = None): """保存设置到文件 \n :param path: ini文件的路径,传入 'default' 保存到默认ini文件 - :return: 当前对象 + :return: 返回当前对象 """ if path == 'default': path = (Path(__file__).parent / 'configs.ini').absolute() @@ -633,7 +690,7 @@ def _session_options_to_dict(options: Union[dict, SessionOptions, None]) -> Unio if val is not None: re_dict[attr] = val - # cert属性默认值为None,未免无法区分是否被设置,主动赋值 + # cert属性默认值为None,未免无法区分是否被设置,故主动赋值 re_dict['cert'] = options.__getattribute__('_cert') return re_dict diff --git a/DrissionPage/configs.ini b/DrissionPage/configs.ini index 9087ada..f97ca93 100644 --- a/DrissionPage/configs.ini +++ b/DrissionPage/configs.ini @@ -1,11 +1,11 @@ [paths] -chromedriver_path = -global_tmp_path = +chromedriver_path = +tmp_path = [chrome_options] debugger_address = -binary_location = -arguments = ['--no-sandbox', '--disable-gpu', '--ignore-certificate-errors', '--headless', '--disable-infobars'] +binary_location = +arguments = ['--no-sandbox', '--disable-gpu', '--ignore-certificate-errors', '--disable-infobars'] extensions = [] experimental_options = {'prefs': {'profile.default_content_settings.popups': 0, 'profile.default_content_setting_values': {'notifications': 2}, 'plugins.plugins_list': [{'enabled': False, 'name': 'Chrome PDF Viewer'}]}, 'useAutomationExtension': False, 'excludeSwitches': ['enable-automation']} diff --git a/DrissionPage/drission.py b/DrissionPage/drission.py index 90e06a5..e3d6dd2 100644 --- a/DrissionPage/drission.py +++ b/DrissionPage/drission.py @@ -55,7 +55,7 @@ class Drission(object): # ------------------处理driver options---------------------- if driver_or_options is None: self._driver_options = om.chrome_options - self._driver_options['driver_path'] = om.paths.get('chromedriver_path', None) + self._driver_options['driver_path'] = om.get_value('paths', 'chromedriver_path') else: # 若接收到WebDriver对象,直接记录 diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index 593789e..59e5c52 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -65,7 +65,7 @@ def set_paths(driver_path: str = None, om.set_item('chrome_options', 'debugger_address', format_path(debugger_address)) if tmp_path is not None: - om.set_item('paths', 'global_tmp_path', format_path(tmp_path)) + om.set_item('paths', 'tmp_path', format_path(tmp_path)) if download_path is not None: experimental_options = om.get_value('chrome_options', 'experimental_options') diff --git a/README.en.md b/README.en.md index 3315e24..9219e50 100644 --- a/README.en.md +++ b/README.en.md @@ -405,7 +405,7 @@ In addition to the above two paths, this method can also set the following paths ```python debugger_address # Debug browser address, such as: 127.0.0.1:9222 download_path # Download file path -global_tmp_path # Temporary folder path +tmp_path # Temporary folder path user_data_path # User data path cache_path # cache path ``` @@ -869,7 +869,7 @@ The ini file has three parts by default: paths, chrome_options, and session_opti ; chromedriver.exe path chromedriver_path = ; Temporary folder path, used to save screenshots, file downloads, etc. -global_tmp_path = +tmp_path = [chrome_options] ; The address and port of the opened browser, such as 127.0.0.1:9222 @@ -2833,7 +2833,7 @@ Parameter Description: - download_path: str-download file path -- global_tmp_path: str-Temporary folder path +- tmp_path: str-Temporary folder path - user_data_path: str-user data path diff --git a/README.zh-cn.md b/README.zh-cn.md index 3b9a646..7c0c6d4 100644 --- a/README.zh-cn.md +++ b/README.zh-cn.md @@ -407,7 +407,7 @@ Message: session not created: Chrome version must be between 70 and 73 ```python debugger_address # 调试浏览器地址,如:127.0.0.1:9222 download_path # 下载文件路径 -global_tmp_path # 临时文件夹路径 +tmp_path # 临时文件夹路径 user_data_path # 用户数据路径 cache_path # 缓存路径 ``` @@ -887,7 +887,7 @@ ini 文件默认拥有三部分配置:paths、chrome_options、session_options ; chromedriver.exe路径 chromedriver_path = ; 临时文件夹路径,用于保存截图、文件下载等 -global_tmp_path = +tmp_path = [chrome_options] ; 已打开的浏览器地址和端口,如127.0.0.1:9222 @@ -2802,15 +2802,15 @@ chrome 配置太复杂,所以把常用的配置写成简单的方法,调用 参数说明: -- driver_path: str - chromedriver.exe 路径 -- chrome_path: str - chrome.exe 路径 +- driver_path: str - chromedriver.exe 路径 +- chrome_path: str - chrome.exe 路径 - debugger_address: str - 调试浏览器地址,例:127.0.0.1:9222 -- download_path: str - 下载文件路径 -- global_tmp_path: str - 临时文件夹路径 -- user_data_path: str - 用户数据路径 -- cache_path: str - 缓存路径 -- ini_path: str - ini 文件路径,为 None 则保存到默认 ini 文件 -- check_version: bool - 是否检查 chromedriver 和 chrome 是否匹配 +- download_path: str - 下载文件路径 +- tmp_path: str - 临时文件夹路径 +- user_data_path: str - 用户数据路径 +- cache_path: str - 缓存路径 +- ini_path: str - ini 文件路径,为 None 则保存到默认 ini 文件 +- check_version: bool - 是否检查 chromedriver 和 chrome 是否匹配 返回: None