修复切换模式headers设置问题

This commit is contained in:
g1879 2023-12-11 01:09:04 +08:00
parent 173a323a42
commit 66e52f6fbe
7 changed files with 14 additions and 12 deletions

View File

@ -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'

View File

@ -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())

View File

@ -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']}

View File

@ -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())

View File

@ -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

View File

@ -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: ...

View File

@ -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.",