mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
设置headers时可接收文本格式,待测试
This commit is contained in:
parent
e68b415edd
commit
b51b2b14fe
@ -11,7 +11,7 @@ from requests import Session
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from .options_manage import OptionsManager
|
||||
from .._functions.web import cookies_to_tuple, set_session_cookies
|
||||
from .._functions.web import cookies_to_tuple, set_session_cookies, format_headers
|
||||
|
||||
|
||||
class SessionOptions(object):
|
||||
@ -171,6 +171,7 @@ class SessionOptions(object):
|
||||
self._headers = None
|
||||
self._del_set.add('headers')
|
||||
else:
|
||||
headers = format_headers(headers)
|
||||
self._headers = {key.lower(): headers[key] for key in headers}
|
||||
return self
|
||||
|
||||
|
@ -49,7 +49,7 @@ class SessionOptions(object):
|
||||
@property
|
||||
def headers(self) -> dict: ...
|
||||
|
||||
def set_headers(self, headers: Union[dict, None]) -> SessionOptions: ...
|
||||
def set_headers(self, headers: Union[dict, str, None]) -> SessionOptions: ...
|
||||
|
||||
def set_a_header(self, name: str, value: str) -> SessionOptions: ...
|
||||
|
||||
|
@ -37,14 +37,15 @@ class SessionElement(DrissionElement):
|
||||
attrs = [f"{k}='{v}'" for k, v in self.attrs.items()]
|
||||
return f'<SessionElement {self.tag} {" ".join(attrs)}>'
|
||||
|
||||
def __call__(self, locator, timeout=None):
|
||||
def __call__(self, locator, index=1, timeout=None):
|
||||
"""在内部查找元素
|
||||
例:ele2 = ele1('@id=ele_id')
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 第几个元素,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 不起实际作用
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
return self.ele(locator)
|
||||
return self.ele(locator, index=index)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.xpath == getattr(other, 'xpath', None)
|
||||
|
@ -394,3 +394,18 @@ def tree(ele_or_page):
|
||||
attrs = ' '.join([f"{k}='{v}'" for k, v in ele.attrs.items()])
|
||||
print(f'<{ele.tag} {attrs}>'.replace('\n', ' '))
|
||||
_tree(ele)
|
||||
|
||||
|
||||
def format_headers(txt):
|
||||
"""从浏览器复制的文本生成dict格式headers,文本用换行分隔
|
||||
:param txt: 从浏览器复制的原始文本格式headers
|
||||
:return: dict格式headers
|
||||
"""
|
||||
if not isinstance(txt, str):
|
||||
return txt
|
||||
headers = {}
|
||||
for header in txt.split('\n'):
|
||||
if header:
|
||||
name, value = header.split(': ', maxsplit=1)
|
||||
headers[name] = value
|
||||
return headers
|
||||
|
@ -52,4 +52,7 @@ def is_cookie_in_driver(page: ChromiumBase, cookie: dict) -> bool: ...
|
||||
def get_blob(page: ChromiumBase, url: str, as_bytes: bool = True) -> bytes: ...
|
||||
|
||||
|
||||
def tree(ele_or_page:BaseParser) -> None: ...
|
||||
def tree(ele_or_page: BaseParser) -> None: ...
|
||||
|
||||
|
||||
def format_headers(txt: str) -> dict: ...
|
||||
|
@ -17,7 +17,7 @@ from tldextract import extract
|
||||
from .._base.base import BasePage
|
||||
from .._configs.session_options import SessionOptions
|
||||
from .._elements.session_element import SessionElement, make_session_ele
|
||||
from .._functions.web import cookie_to_dict
|
||||
from .._functions.web import cookie_to_dict, format_headers
|
||||
from .._units.setter import SessionPageSetter
|
||||
|
||||
|
||||
@ -293,7 +293,7 @@ class SessionPage(BasePage):
|
||||
if 'headers' not in kwargs:
|
||||
kwargs['headers'] = {}
|
||||
else:
|
||||
kwargs['headers'] = CaseInsensitiveDict(kwargs['headers'])
|
||||
kwargs['headers'] = CaseInsensitiveDict(format_headers(kwargs['headers']))
|
||||
|
||||
# 设置referer和host值
|
||||
parsed_url = urlparse(url)
|
||||
|
@ -79,7 +79,7 @@ class SessionPage(BasePage):
|
||||
params: dict | None = ...,
|
||||
data: Union[dict, str, None] = ...,
|
||||
json: Union[dict, str, None] = ...,
|
||||
headers: dict | None = ...,
|
||||
headers: Union[dict, str, None] = ...,
|
||||
cookies: Any | None = ...,
|
||||
files: Any | None = ...,
|
||||
auth: Any | None = ...,
|
||||
@ -140,7 +140,7 @@ class SessionPage(BasePage):
|
||||
timeout: float | None = ...,
|
||||
params: dict | None = ...,
|
||||
json: Union[dict, str, None] = ...,
|
||||
headers: dict | None = ...,
|
||||
headers: Union[dict, str, None] = ...,
|
||||
cookies: Any | None = ...,
|
||||
files: Any | None = ...,
|
||||
auth: Any | None = ...,
|
||||
|
@ -90,7 +90,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
||||
params: dict | None = ...,
|
||||
data: Union[dict, str, None] = ...,
|
||||
json: Union[dict, str, None] = ...,
|
||||
headers: dict | None = ...,
|
||||
headers: Union[dict, str, None] = ...,
|
||||
cookies: Any | None = ...,
|
||||
files: Any | None = ...,
|
||||
auth: Any | None = ...,
|
||||
@ -151,7 +151,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
||||
timeout: float | None = ...,
|
||||
params: dict | None = ...,
|
||||
json: Union[dict, str, None] = ...,
|
||||
headers: dict | None = ...,
|
||||
headers: Union[dict, str, None] = ...,
|
||||
cookies: Any | None = ...,
|
||||
files: Any | None = ...,
|
||||
auth: Any | None = ...,
|
||||
|
@ -12,6 +12,7 @@ from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from .cookies_setter import SessionCookiesSetter, CookiesSetter, WebPageCookiesSetter
|
||||
from .._functions.tools import show_or_hide_browser
|
||||
from .._functions.web import format_headers
|
||||
|
||||
|
||||
class BasePageSetter(object):
|
||||
@ -134,13 +135,13 @@ class ChromiumBaseSetter(BasePageSetter):
|
||||
files = (files, )
|
||||
self._page._upload_list = [str(Path(i).absolute()) for i in files]
|
||||
|
||||
def headers(self, headers: dict) -> None:
|
||||
def headers(self, headers) -> None:
|
||||
"""设置固定发送的headers
|
||||
:param headers: dict格式的headers数据
|
||||
:return: None
|
||||
"""
|
||||
self._page.run_cdp('Network.enable')
|
||||
self._page.run_cdp('Network.setExtraHTTPHeaders', headers=headers)
|
||||
self._page.run_cdp('Network.setExtraHTTPHeaders', headers=format_headers(headers))
|
||||
|
||||
def auto_handle_alert(self, on_off=True, accept=True):
|
||||
"""设置是否启用自动处理弹窗
|
||||
@ -293,7 +294,7 @@ class SessionPageSetter(BasePageSetter):
|
||||
:param headers: dict形式的headers
|
||||
:return: None
|
||||
"""
|
||||
self._page._headers = CaseInsensitiveDict(headers)
|
||||
self._page._headers = CaseInsensitiveDict(format_headers(headers))
|
||||
|
||||
def header(self, name, value):
|
||||
"""设置headers中一个项
|
||||
|
@ -58,7 +58,7 @@ class ChromiumBaseSetter(BasePageSetter):
|
||||
|
||||
def local_storage(self, item: str, value: Union[str, bool]) -> None: ...
|
||||
|
||||
def headers(self, headers: dict) -> None: ...
|
||||
def headers(self, headers: Union[dict, str]) -> None: ...
|
||||
|
||||
def auto_handle_alert(self, on_off: bool = True, accept: bool = True) -> None: ...
|
||||
|
||||
@ -111,7 +111,7 @@ class SessionPageSetter(BasePageSetter):
|
||||
|
||||
def encoding(self, encoding: Union[str, None], set_all: bool = True) -> None: ...
|
||||
|
||||
def headers(self, headers: dict) -> None: ...
|
||||
def headers(self, headers: Union[str, dict]) -> None: ...
|
||||
|
||||
def header(self, name: str, value: str) -> None: ...
|
||||
|
||||
@ -145,7 +145,7 @@ class WebPageSetter(ChromiumPageSetter):
|
||||
|
||||
def user_agent(self, ua: str, platform: str = None) -> None: ...
|
||||
|
||||
def headers(self, headers: dict) -> None: ...
|
||||
def headers(self, headers: Union[str, dict]) -> None: ...
|
||||
|
||||
@property
|
||||
def cookies(self) -> WebPageCookiesSetter: ...
|
||||
@ -158,7 +158,7 @@ class WebPageTabSetter(TabSetter):
|
||||
|
||||
def user_agent(self, ua: str, platform: str = None) -> None: ...
|
||||
|
||||
def headers(self, headers: dict) -> None: ...
|
||||
def headers(self, headers: Union[str, dict]) -> None: ...
|
||||
|
||||
@property
|
||||
def cookies(self) -> WebPageCookiesSetter: ...
|
||||
|
Loading…
x
Reference in New Issue
Block a user