global_tmp_path改名为tmp_path

This commit is contained in:
g1879 2020-11-25 22:35:29 +08:00
parent 9d67fe74a4
commit 2f019fb4ba
6 changed files with 98 additions and 41 deletions

View File

@ -1,6 +1,6 @@
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
""" """
配置文件 管理配置的类
@Author : g1879 @Author : g1879
@Contact : g1879@qq.com @Contact : g1879@qq.com
@File : config.py @File : config.py
@ -28,10 +28,10 @@ class OptionsManager(object):
self._chrome_options = None self._chrome_options = None
self._session_options = None self._session_options = None
if 'global_tmp_path' not in self.paths or not self.get_value('paths', 'global_tmp_path'): if 'tmp_path' not in self.paths or not self.get_value('paths', 'tmp_path'):
global_tmp_path = str((Path(__file__).parent / 'tmp').absolute()) tmp_path = str((Path(__file__).parent / 'tmp').absolute())
Path(global_tmp_path).mkdir(parents=True, exist_ok=True) Path(tmp_path).mkdir(parents=True, exist_ok=True)
self.set_item('paths', 'global_tmp_path', global_tmp_path) self.set_item('paths', 'tmp_path', tmp_path)
self.save(self.ini_path) self.save(self.ini_path)
def __text__(self) -> str: def __text__(self) -> str:
@ -128,9 +128,9 @@ class OptionsManager(object):
class SessionOptions(object): class SessionOptions(object):
def __init__(self, read_file: bool = True, ini_path: str = None): def __init__(self, read_file: bool = True, ini_path: str = None):
""" """requests的Session对象配置类 \n
:param read_file: :param read_file: 是否从文件读取配置
:param ini_path: :param ini_path: ini文件路径
""" """
self.ini_path = None self.ini_path = None
self._headers = None self._headers = None
@ -187,115 +187,168 @@ class SessionOptions(object):
if options_dict.get('max_redirects', None) is not None: if options_dict.get('max_redirects', None) is not None:
self._headers = options_dict['max_redirects'] 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 @property
def headers(self) -> dict: def headers(self) -> dict:
"""返回headers设置信息"""
return self._headers return self._headers
@property @property
def cookies(self) -> dict: def cookies(self) -> dict:
"""返回cookies设置信息"""
return self._cookies return self._cookies
@property @property
def auth(self) -> tuple: def auth(self) -> tuple:
"""返回auth设置信息"""
return self._auth return self._auth
@property @property
def proxies(self) -> dict: def proxies(self) -> dict:
"""返回proxies设置信息"""
return self._proxies return self._proxies
@property @property
def hooks(self) -> dict: def hooks(self) -> dict:
"""返回hooks设置信息"""
return self._hooks return self._hooks
@property @property
def params(self) -> dict: def params(self) -> dict:
"""返回params设置信息"""
return self._params return self._params
@property @property
def verify(self) -> bool: def verify(self) -> bool:
"""返回verify设置信息"""
return self._verify return self._verify
@property @property
def cert(self) -> Union[str, tuple]: def cert(self) -> Union[str, tuple]:
"""返回cert设置信息"""
return self._cert return self._cert
@property @property
def adapters(self): def adapters(self):
"""返回adapters设置信息"""
return self._adapters return self._adapters
@property @property
def stream(self) -> bool: def stream(self) -> bool:
"""返回stream设置信息"""
return self._stream return self._stream
@property @property
def trust_env(self) -> bool: def trust_env(self) -> bool:
"""返回trust_env设置信息"""
return self._trust_env return self._trust_env
@property @property
def max_redirects(self) -> int: def max_redirects(self) -> int:
"""返回max_redirects设置信息"""
return self._max_redirects return self._max_redirects
@headers.setter @headers.setter
def headers(self, headers: dict) -> None: def headers(self, headers: dict) -> None:
"""设置headers参数 \n
:param headers: 参数值
:return: None
"""
self._headers = {key.lower(): headers[key] for key in headers} self._headers = {key.lower(): headers[key] for key in headers}
@cookies.setter @cookies.setter
def cookies(self, cookies: dict) -> None: def cookies(self, cookies: dict) -> None:
"""设置cookies参数 \n
:param cookies: 参数值
:return: None
"""
self._cookies = cookies self._cookies = cookies
@auth.setter @auth.setter
def auth(self, auth: tuple) -> None: def auth(self, auth: tuple) -> None:
"""设置auth参数 \n
:param auth: 参数值
:return: None
"""
self._auth = auth self._auth = auth
@proxies.setter @proxies.setter
def proxies(self, proxies: dict) -> None: def proxies(self, proxies: dict) -> None:
"""设置proxies参数 \n
:param proxies: 参数值
:return: None
"""
self._proxies = proxies self._proxies = proxies
@hooks.setter @hooks.setter
def hooks(self, hooks: dict) -> None: def hooks(self, hooks: dict) -> None:
"""设置hooks参数 \n
:param hooks: 参数值
:return: None
"""
self._hooks = hooks self._hooks = hooks
@params.setter @params.setter
def params(self, params: dict) -> None: def params(self, params: dict) -> None:
"""设置params参数 \n
:param params: 参数值
:return: None
"""
self._params = params self._params = params
@verify.setter @verify.setter
def verify(self, verify: bool) -> None: def verify(self, verify: bool) -> None:
"""设置verify参数 \n
:param verify: 参数值
:return: None
"""
self._verify = verify self._verify = verify
@cert.setter @cert.setter
def cert(self, cert: Union[str, tuple]) -> None: def cert(self, cert: Union[str, tuple]) -> None:
"""设置cert参数 \n
:param cert: 参数值
:return: None
"""
self._cert = cert self._cert = cert
@adapters.setter @adapters.setter
def adapters(self, adapters) -> None: def adapters(self, adapters) -> None:
"""设置 \n
:param adapters: 参数值
:return: None
"""
self._adapters = adapters self._adapters = adapters
@stream.setter @stream.setter
def stream(self, stream: bool) -> None: def stream(self, stream: bool) -> None:
"""设置stream参数 \n
:param stream: 参数值
:return: None
"""
self._stream = stream self._stream = stream
@trust_env.setter @trust_env.setter
def trust_env(self, trust_env: bool) -> None: def trust_env(self, trust_env: bool) -> None:
"""设置trust_env参数 \n
:param trust_env: 参数值
:return: None
"""
self._trust_env = trust_env self._trust_env = trust_env
@max_redirects.setter @max_redirects.setter
def max_redirects(self, max_redirects: int) -> None: def max_redirects(self, max_redirects: int) -> None:
"""设置max_redirects参数 \n
:param max_redirects: 参数值
:return: None
"""
self._max_redirects = max_redirects self._max_redirects = max_redirects
def set_header(self, attr: str, value: str): def set_header(self, attr: str, value: str):
"""设置header中一个项 \n
:param attr: 设置名称
:param value: 设置值
:return: 返回当前对象
"""
if self._headers is None: if self._headers is None:
self._headers = {} self._headers = {}
@ -303,6 +356,10 @@ class SessionOptions(object):
return self return self
def remove_header(self, attr: str): def remove_header(self, attr: str):
"""从headers中删除一个设置 \n
:param attr: 要删除的设置
:return: 返回当前对象
"""
if self._headers is None: if self._headers is None:
return self return self
@ -315,7 +372,7 @@ class SessionOptions(object):
def save(self, path: str = None): def save(self, path: str = None):
"""保存设置到文件 \n """保存设置到文件 \n
:param path: ini文件的路径传入 'default' 保存到默认ini文件 :param path: ini文件的路径传入 'default' 保存到默认ini文件
:return: 当前对象 :return: 返回当前对象
""" """
if path == 'default': if path == 'default':
path = (Path(__file__).parent / 'configs.ini').absolute() 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: if val is not None:
re_dict[attr] = val re_dict[attr] = val
# cert属性默认值为None未免无法区分是否被设置主动赋值 # cert属性默认值为None未免无法区分是否被设置主动赋值
re_dict['cert'] = options.__getattribute__('_cert') re_dict['cert'] = options.__getattribute__('_cert')
return re_dict return re_dict

View File

@ -1,11 +1,11 @@
[paths] [paths]
chromedriver_path = chromedriver_path =
global_tmp_path = tmp_path =
[chrome_options] [chrome_options]
debugger_address = debugger_address =
binary_location = binary_location =
arguments = ['--no-sandbox', '--disable-gpu', '--ignore-certificate-errors', '--headless', '--disable-infobars'] arguments = ['--no-sandbox', '--disable-gpu', '--ignore-certificate-errors', '--disable-infobars']
extensions = [] 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']} 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']}

View File

@ -55,7 +55,7 @@ class Drission(object):
# ------------------处理driver options---------------------- # ------------------处理driver options----------------------
if driver_or_options is None: if driver_or_options is None:
self._driver_options = om.chrome_options 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: else:
# 若接收到WebDriver对象直接记录 # 若接收到WebDriver对象直接记录

View File

@ -65,7 +65,7 @@ def set_paths(driver_path: str = None,
om.set_item('chrome_options', 'debugger_address', format_path(debugger_address)) om.set_item('chrome_options', 'debugger_address', format_path(debugger_address))
if tmp_path is not None: 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: if download_path is not None:
experimental_options = om.get_value('chrome_options', 'experimental_options') experimental_options = om.get_value('chrome_options', 'experimental_options')

View File

@ -405,7 +405,7 @@ In addition to the above two paths, this method can also set the following paths
```python ```python
debugger_address # Debug browser address, such as: 127.0.0.1:9222 debugger_address # Debug browser address, such as: 127.0.0.1:9222
download_path # Download file path download_path # Download file path
global_tmp_path # Temporary folder path tmp_path # Temporary folder path
user_data_path # User data path user_data_path # User data path
cache_path # cache 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.exe path
chromedriver_path = chromedriver_path =
; Temporary folder path, used to save screenshots, file downloads, etc. ; Temporary folder path, used to save screenshots, file downloads, etc.
global_tmp_path = tmp_path =
[chrome_options] [chrome_options]
; The address and port of the opened browser, such as 127.0.0.1:9222 ; 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 - 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 - user_data_path: str-user data path

View File

@ -407,7 +407,7 @@ Message: session not created: Chrome version must be between 70 and 73
```python ```python
debugger_address # 调试浏览器地址127.0.0.1:9222 debugger_address # 调试浏览器地址127.0.0.1:9222
download_path # 下载文件路径 download_path # 下载文件路径
global_tmp_path # 临时文件夹路径 tmp_path # 临时文件夹路径
user_data_path # 用户数据路径 user_data_path # 用户数据路径
cache_path # 缓存路径 cache_path # 缓存路径
``` ```
@ -887,7 +887,7 @@ ini 文件默认拥有三部分配置paths、chrome_options、session_options
; chromedriver.exe路径 ; chromedriver.exe路径
chromedriver_path = chromedriver_path =
; 临时文件夹路径,用于保存截图、文件下载等 ; 临时文件夹路径,用于保存截图、文件下载等
global_tmp_path = tmp_path =
[chrome_options] [chrome_options]
; 已打开的浏览器地址和端口如127.0.0.1:9222 ; 已打开的浏览器地址和端口如127.0.0.1:9222
@ -2802,15 +2802,15 @@ chrome 配置太复杂,所以把常用的配置写成简单的方法,调用
参数说明: 参数说明:
- driver_path: str - chromedriver.exe 路径 - driver_path: str - chromedriver.exe 路径
- chrome_path: str - chrome.exe 路径 - chrome_path: str - chrome.exe 路径
- debugger_address: str - 调试浏览器地址127.0.0.1:9222 - debugger_address: str - 调试浏览器地址127.0.0.1:9222
- download_path: str - 下载文件路径 - download_path: str - 下载文件路径
- global_tmp_path: str - 临时文件夹路径 - tmp_path: str - 临时文件夹路径
- user_data_path: str - 用户数据路径 - user_data_path: str - 用户数据路径
- cache_path: str - 缓存路径 - cache_path: str - 缓存路径
- ini_path: str - ini 文件路径,为 None 则保存到默认 ini 文件 - ini_path: str - ini 文件路径,为 None 则保存到默认 ini 文件
- check_version: bool - 是否检查 chromedriver 和 chrome 是否匹配 - check_version: bool - 是否检查 chromedriver 和 chrome 是否匹配
返回: None 返回: None