添加直接从配置对象生成Session对象的方法

This commit is contained in:
g1879 2023-02-22 23:20:49 +08:00
parent 415ef28dc6
commit bccb20b84a
6 changed files with 34 additions and 29 deletions

View File

@ -538,6 +538,7 @@ class ChromiumDownloadSetter(DownloadSetter):
downloadPath=self._page.download_path) downloadPath=self._page.download_path)
sleep(2) sleep(2)
self._page.browser_driver.Browser.setDownloadBehavior(behavior='deny', eventsEnabled=True) self._page.browser_driver.Browser.setDownloadBehavior(behavior='deny', eventsEnabled=True)
else: else:
self._page.browser_driver.Browser.cancelDownload(guid=kwargs['guid']) self._page.browser_driver.Browser.cancelDownload(guid=kwargs['guid'])
self._page.download.add(file_url=url, goal_path=self._page.download_path, self._page.download.add(file_url=url, goal_path=self._page.download_path,

View File

@ -58,29 +58,7 @@ class SessionPage(BasePage):
def _create_session(self): def _create_session(self):
"""创建内建Session对象""" """创建内建Session对象"""
if not self._session: if not self._session:
self._set_session(self._session_options) self._session = set_session(self._session_options)
def _set_session(self, opt):
"""根据传入字典对session进行设置
:param opt: session配置字典
:return: None
"""
self._session = Session()
if opt.headers:
self._session.headers = CaseInsensitiveDict(opt.headers)
if opt.cookies:
self.set.cookies(opt.cookies)
if opt.adapters:
for url, adapter in opt.adapters:
self._session.mount(url, adapter)
attrs = ['auth', 'proxies', 'hooks', 'params', 'verify',
'cert', 'stream', 'trust_env', 'max_redirects']
for i in attrs:
attr = opt.__getattribute__(i)
if attr:
self._session.__setattr__(i, attr)
def __call__(self, loc_or_str, timeout=None): def __call__(self, loc_or_str, timeout=None):
"""在内部查找元素 """在内部查找元素
@ -555,3 +533,28 @@ def set_charset(response) -> Response:
response.encoding = charset response.encoding = charset
return response return response
def set_session(opt):
"""根据传入配置对象创建Session对象
:param opt: SessionOptions对象
:return: Session
"""
s = Session()
if opt.headers:
s.headers = CaseInsensitiveDict(opt.headers)
if opt.cookies:
set_session_cookies(s, opt.cookies)
if opt.adapters:
for url, adapter in opt.adapters:
s.mount(url, adapter)
attrs = ['auth', 'proxies', 'hooks', 'params', 'verify',
'cert', 'stream', 'trust_env', 'max_redirects']
for i in attrs:
attr = opt.__getattribute__(i)
if attr:
s.__setattr__(i, attr)
return s

View File

@ -41,8 +41,6 @@ class SessionPage(BasePage):
def _create_session(self) -> None: ... def _create_session(self) -> None: ...
def _set_session(self, opt: SessionOptions) -> None: ...
def _set_runtime_settings(self) -> None: ... def _set_runtime_settings(self) -> None: ...
def __call__(self, def __call__(self,
@ -103,7 +101,7 @@ class SessionPage(BasePage):
def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ...
def _find_elements(self, loc_or_ele: Union[Tuple[str, str], str, SessionElement], def _find_elements(self, loc_or_ele: Union[Tuple[str, str], str, SessionElement],
timeout: float = None, single: bool = True, raise_err: bool =None)\ timeout: float = None, single: bool = True, raise_err: bool = None) \
-> Union[SessionElement, str, NoneElement, List[Union[SessionElement, str]]]: ... -> Union[SessionElement, str, NoneElement, List[Union[SessionElement, str]]]: ...
def get_cookies(self, def get_cookies(self,
@ -231,3 +229,6 @@ def check_headers(kwargs: Union[dict, CaseInsensitiveDict], headers: Union[dict,
def set_charset(response: Response) -> Response: ... def set_charset(response: Response) -> Response: ...
def set_session(opt: SessionOptions) -> Session: ...

View File

@ -184,7 +184,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
def session(self): def session(self):
"""返回Session对象如未初始化则按配置信息创建""" """返回Session对象如未初始化则按配置信息创建"""
if self._session is None: if self._session is None:
self._set_session(self._session_options) self._create_session()
return self._session return self._session
@property @property

View File

@ -2,7 +2,7 @@ requests
tldextract tldextract
lxml lxml
cssselect cssselect
DownloadKit>=0.5.0 DownloadKit>=0.5.2
FlowViewer>=0.2.1 FlowViewer>=0.2.1
websocket-client websocket-client
click click

View File

@ -22,7 +22,7 @@ setup(
"lxml", "lxml",
"tldextract", "tldextract",
"requests", "requests",
"DownloadKit>=0.5.0", "DownloadKit>=0.5.2",
"FlowViewer", "FlowViewer",
"websocket-client", "websocket-client",
'click~=8.1.3' 'click~=8.1.3'