From fb580fda007523e7d1126e12f1101f0efb2d5d57 Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 2 Jun 2023 21:40:33 +0800 Subject: [PATCH] =?UTF-8?q?session=E7=9A=84=E7=BD=91=E5=9D=80=E4=BC=9A?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8E=BB=E9=99=A4http://=E5=92=8Chttps://?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/configs/session_options.py | 15 ++++++++++++--- DrissionPage/session_page.py | 15 ++++++++++++--- DrissionPage/session_page.pyi | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/DrissionPage/configs/session_options.py b/DrissionPage/configs/session_options.py index eaa763b..8e32598 100644 --- a/DrissionPage/configs/session_options.py +++ b/DrissionPage/configs/session_options.py @@ -110,14 +110,23 @@ class SessionOptions(object): self._proxies = {} return self._proxies - def set_proxies(self, http, https=None): + def set_proxies(self, http=None, https=None): """设置proxies参数 :param http: http代理地址 :param https: https代理地址 :return: 返回当前对象 """ - proxies = None if http == https is None else {'http': http, 'https': https or http} - self._sets('proxies', proxies) + if isinstance(http, str): + if http.startswith('http://'): + http = http[7:] + elif http.startswith('https://'): + http = http[8:] + if isinstance(https, str): + if https.startswith('http://'): + https = https[7:] + elif http.startswith('https://'): + https = https[8:] + self._sets('proxies', {'http': http, 'https': https}) return self # ===========须独立处理的项结束============ diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 20e1f04..71ccb88 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -366,14 +366,23 @@ class SessionPageSetter(object): """ self._page.session.headers['user-agent'] = ua - def proxies(self, http, https=None): + def proxies(self, http=None, https=None): """设置proxies参数 :param http: http代理地址 :param https: https代理地址 :return: None """ - proxies = None if http == https is None else {'http': http, 'https': https or http} - self._page.session.proxies = proxies + if isinstance(http, str): + if http.startswith('http://'): + http = http[7:] + elif http.startswith('https://'): + http = http[8:] + if isinstance(https, str): + if https.startswith('http://'): + https = https[7:] + elif https.startswith('https://'): + https = https[8:] + self._page.session.proxies = {'http': http, 'https': https} def auth(self, auth): """设置认证元组或对象 diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index c13d488..41bacd7 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -177,7 +177,7 @@ class SessionPageSetter(object): def user_agent(self, ua: str) -> None: ... - def proxies(self, http, https=None) -> None: ... + def proxies(self, http: str = None, https: str = None) -> None: ... def auth(self, auth: Union[Tuple[str, str], HTTPBasicAuth, None]) -> None: ...