diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 5861945..1c99f50 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -13,4 +13,4 @@ from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] -__version__ = '4.0.0b19' +__version__ = '4.0.0b20' diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index 907c60b..983ee4a 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -304,7 +304,7 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): self.get(url) def cookies_to_session(self, copy_user_agent=True): - """把driver对象的cookies复制到session对象 + """把浏览器的cookies复制到session对象 :param copy_user_agent: 是否复制ua信息 :return: None """ @@ -313,7 +313,7 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): if copy_user_agent: user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] - self.session.headers.update({"User-Agent": user_agent}) + self._headers.update({"User-Agent": user_agent}) set_session_cookies(self.session, super(SessionPage, self).get_cookies()) diff --git a/DrissionPage/_pages/session_page.py b/DrissionPage/_pages/session_page.py index 60d49f5..038bf38 100644 --- a/DrissionPage/_pages/session_page.py +++ b/DrissionPage/_pages/session_page.py @@ -49,6 +49,8 @@ class SessionPage(BasePage): elif isinstance(session_or_options, Session): self._session_options = SessionOptions() + self._headers = session_or_options.headers + session_or_options.headers = None self._session = session_or_options def _s_set_runtime_settings(self): @@ -305,12 +307,12 @@ class SessionPage(BasePage): parsed_url = urlparse(url) hostname = parsed_url.hostname scheme = parsed_url.scheme - if not check_headers(kwargs, self.session.headers, 'Referer'): + if not check_headers(kwargs, self._headers, 'Referer'): kwargs['headers']['Referer'] = self.url if self.url else f'{scheme}://{hostname}' if 'Host' not in kwargs['headers']: kwargs['headers']['Host'] = hostname - if not check_headers(kwargs, self.session.headers, 'timeout'): + if not check_headers(kwargs, self._headers, 'timeout'): kwargs['timeout'] = self.timeout kwargs['headers'] = {**self._headers, **kwargs['headers']} diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index 748ccf7..3b08d1a 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -274,7 +274,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): if copy_user_agent: user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] - self.session.headers.update({"User-Agent": user_agent}) + self._headers.update({"User-Agent": user_agent}) set_session_cookies(self.session, super(SessionPage, self).get_cookies()) diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 705bc1d..cfca821 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -253,12 +253,12 @@ class SessionPageSetter(BasePageSetter): def encoding(self, encoding, set_all=True): """设置编码 - :param encoding: 编码名称 - :param set_all: 是否设置对象参数,为False则只设置当前response + :param encoding: 编码名称,如果要取消之前的设置,传入None + :param set_all: 是否设置对象参数,为False则只设置当前Response :return: None """ if set_all: - self._page._encoding = encoding + self._page._encoding = encoding if encoding else None if self._page.response: self._page.response.encoding = encoding diff --git a/DrissionPage/_units/setter.pyi b/DrissionPage/_units/setter.pyi index a82eaeb..45faf59 100644 --- a/DrissionPage/_units/setter.pyi +++ b/DrissionPage/_units/setter.pyi @@ -5,7 +5,7 @@ """ from http.cookiejar import Cookie from pathlib import Path -from typing import Union, Tuple, Literal, Any +from typing import Union, Tuple, Literal, Any, Optional from requests.adapters import HTTPAdapter from requests.auth import HTTPBasicAuth @@ -100,7 +100,7 @@ class SessionPageSetter(BasePageSetter): def timeout(self, second: float) -> None: ... - def encoding(self, encoding: str, set_all: bool = True) -> None: ... + def encoding(self, encoding: Optional[str, None], set_all: bool = True) -> None: ... def cookie(self, cookie: Union[Cookie, str, dict]) -> None: ... diff --git a/setup.py b/setup.py index 1ca634c..ed2a7d6 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh: setup( name="DrissionPage", - version="4.0.0b19", + version="4.0.0b20", author="g1879", author_email="g1879@qq.com", description="Python based web automation tool. It can control the browser and send and receive data packets.",