From bccb20b84a1a5a1d2b157830c47dc8d4bc9f2c31 Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 22 Feb 2023 23:20:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9B=B4=E6=8E=A5=E4=BB=8E?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=AF=B9=E8=B1=A1=E7=94=9F=E6=88=90Session?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_page.py | 1 + DrissionPage/session_page.py | 49 +++++++++++++++++++---------------- DrissionPage/session_page.pyi | 7 ++--- DrissionPage/web_page.py | 2 +- requirements.txt | 2 +- setup.py | 2 +- 6 files changed, 34 insertions(+), 29 deletions(-) diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index d135056..fce221e 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -538,6 +538,7 @@ class ChromiumDownloadSetter(DownloadSetter): downloadPath=self._page.download_path) sleep(2) self._page.browser_driver.Browser.setDownloadBehavior(behavior='deny', eventsEnabled=True) + else: self._page.browser_driver.Browser.cancelDownload(guid=kwargs['guid']) self._page.download.add(file_url=url, goal_path=self._page.download_path, diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index aa702b5..50de5df 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -58,29 +58,7 @@ class SessionPage(BasePage): def _create_session(self): """创建内建Session对象""" if not self._session: - self._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) + self._session = set_session(self._session_options) def __call__(self, loc_or_str, timeout=None): """在内部查找元素 @@ -555,3 +533,28 @@ def set_charset(response) -> Response: response.encoding = charset 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 diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index ebd0dd2..478d4b9 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -41,8 +41,6 @@ class SessionPage(BasePage): def _create_session(self) -> None: ... - def _set_session(self, opt: SessionOptions) -> None: ... - def _set_runtime_settings(self) -> None: ... 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 _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]]]: ... 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_session(opt: SessionOptions) -> Session: ... diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 5ab9a1a..f2aa17f 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -184,7 +184,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def session(self): """返回Session对象,如未初始化则按配置信息创建""" if self._session is None: - self._set_session(self._session_options) + self._create_session() return self._session @property diff --git a/requirements.txt b/requirements.txt index bb59fbd..6ecc155 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ requests tldextract lxml cssselect -DownloadKit>=0.5.0 +DownloadKit>=0.5.2 FlowViewer>=0.2.1 websocket-client click \ No newline at end of file diff --git a/setup.py b/setup.py index 63b909a..a370c67 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ setup( "lxml", "tldextract", "requests", - "DownloadKit>=0.5.0", + "DownloadKit>=0.5.2", "FlowViewer", "websocket-client", 'click~=8.1.3'