From b51b2b14fe8efb2573df4f3cf28d2e4abf7e105a Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 4 Mar 2024 23:38:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AEheaders=E6=97=B6=E5=8F=AF?= =?UTF-8?q?=E6=8E=A5=E6=94=B6=E6=96=87=E6=9C=AC=E6=A0=BC=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E5=BE=85=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_configs/session_options.py | 3 ++- DrissionPage/_configs/session_options.pyi | 2 +- DrissionPage/_elements/session_element.py | 5 +++-- DrissionPage/_functions/web.py | 15 +++++++++++++++ DrissionPage/_functions/web.pyi | 5 ++++- DrissionPage/_pages/session_page.py | 4 ++-- DrissionPage/_pages/session_page.pyi | 4 ++-- DrissionPage/_pages/web_page.pyi | 4 ++-- DrissionPage/_units/setter.py | 7 ++++--- DrissionPage/_units/setter.pyi | 8 ++++---- 10 files changed, 39 insertions(+), 18 deletions(-) diff --git a/DrissionPage/_configs/session_options.py b/DrissionPage/_configs/session_options.py index a3318ed..f3385ee 100644 --- a/DrissionPage/_configs/session_options.py +++ b/DrissionPage/_configs/session_options.py @@ -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 diff --git a/DrissionPage/_configs/session_options.pyi b/DrissionPage/_configs/session_options.pyi index 6af3375..6875e28 100644 --- a/DrissionPage/_configs/session_options.pyi +++ b/DrissionPage/_configs/session_options.pyi @@ -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: ... diff --git a/DrissionPage/_elements/session_element.py b/DrissionPage/_elements/session_element.py index 40a265c..b2479c8 100644 --- a/DrissionPage/_elements/session_element.py +++ b/DrissionPage/_elements/session_element.py @@ -37,14 +37,15 @@ class SessionElement(DrissionElement): attrs = [f"{k}='{v}'" for k, v in self.attrs.items()] return f'' - 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) diff --git a/DrissionPage/_functions/web.py b/DrissionPage/_functions/web.py index 3ef7b1e..4b28a36 100644 --- a/DrissionPage/_functions/web.py +++ b/DrissionPage/_functions/web.py @@ -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 diff --git a/DrissionPage/_functions/web.pyi b/DrissionPage/_functions/web.pyi index aaebf3c..b3a9914 100644 --- a/DrissionPage/_functions/web.pyi +++ b/DrissionPage/_functions/web.pyi @@ -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: ... diff --git a/DrissionPage/_pages/session_page.py b/DrissionPage/_pages/session_page.py index 1bc7c27..b739545 100644 --- a/DrissionPage/_pages/session_page.py +++ b/DrissionPage/_pages/session_page.py @@ -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) diff --git a/DrissionPage/_pages/session_page.pyi b/DrissionPage/_pages/session_page.pyi index 7a3ac69..e229a36 100644 --- a/DrissionPage/_pages/session_page.pyi +++ b/DrissionPage/_pages/session_page.pyi @@ -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 = ..., diff --git a/DrissionPage/_pages/web_page.pyi b/DrissionPage/_pages/web_page.pyi index 0f11253..8ee57bd 100644 --- a/DrissionPage/_pages/web_page.pyi +++ b/DrissionPage/_pages/web_page.pyi @@ -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 = ..., diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 245ac24..d625d95 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -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中一个项 diff --git a/DrissionPage/_units/setter.pyi b/DrissionPage/_units/setter.pyi index 8695d61..4e6f852 100644 --- a/DrissionPage/_units/setter.pyi +++ b/DrissionPage/_units/setter.pyi @@ -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: ...