mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
添加直接从配置对象生成Session对象的方法
This commit is contained in:
parent
415ef28dc6
commit
bccb20b84a
@ -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,
|
||||||
|
@ -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
|
||||||
|
@ -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,
|
||||||
@ -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: ...
|
||||||
|
@ -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
|
||||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user