优化类型注解;加入tree(),未完成

This commit is contained in:
g1879 2024-02-04 17:51:36 +08:00
parent dc440d8e61
commit 001c479e4c
9 changed files with 51 additions and 53 deletions

View File

@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions
from ._configs.session_options import SessionOptions from ._configs.session_options import SessionOptions
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
__version__ = '4.0.4.4' __version__ = '4.0.4.5'

View File

@ -10,7 +10,6 @@ from os.path import basename, sep
from pathlib import Path from pathlib import Path
from re import search from re import search
from time import perf_counter, sleep from time import perf_counter, sleep
from colorama import Fore, init
from DataRecorder.tools import get_usable_path from DataRecorder.tools import get_usable_path
@ -125,33 +124,6 @@ class ChromiumElement(DrissionElement):
"""返回元素内所有文本,文本已格式化""" """返回元素内所有文本,文本已格式化"""
return get_ele_txt(make_session_ele(self.html)) return get_ele_txt(make_session_ele(self.html))
def tree(self):
"""打印当前元素的子元素结构树默认展开层数是5层"""
init()
self.__tree(ele=self)
def __tree(self,ele, layer=5, last_one=False, body=''):
try:
list_ele = ele.children(timeout=0.1)
except:
list_ele = []
length = len(list_ele)
body_unit = ' ' if last_one else ''
tail = '├───'
new_body = body + body_unit
if length > 0 and layer >= 1:
new_last_one = False
for i in range(length):
if i == length - 1:
tail = '└───'
new_last_one = True
e = list_ele[i]
print(f'{Fore.BLUE}{new_body}{tail}{Fore.CYAN}{i}<{e.tag}> {Fore.RESET}{e.attrs}')
self.__tree(e, layer - 1, new_last_one, new_body)
@property @property
def raw_text(self): def raw_text(self):
"""返回未格式化处理的元素内文本""" """返回未格式化处理的元素内文本"""

View File

@ -319,12 +319,12 @@ def is_cookie_in_driver(page, cookie):
:return: bool :return: bool
""" """
if 'domain' in cookie: if 'domain' in cookie:
for c in page.get_cookies(all_domains=True): for c in page.cookies(all_domains=True):
if cookie['name'] == c['name'] and cookie['value'] == c['value'] and cookie['domain'] == c.get('domain', if cookie['name'] == c['name'] and cookie['value'] == c['value'] and cookie['domain'] == c.get('domain',
None): None):
return True return True
else: else:
for c in page.get_cookies(all_domains=True): for c in page.cookies(all_domains=True):
if cookie['name'] == c['name'] and cookie['value'] == c['value']: if cookie['name'] == c['name'] and cookie['value'] == c['value']:
return True return True
return False return False
@ -363,3 +363,26 @@ def get_blob(page, url, as_bytes=True):
return b64decode(result.split(',', 1)[-1]) return b64decode(result.split(',', 1)[-1])
else: else:
return result return result
def tree(ele_or_page, layer=5, last_one=False, body=''):
try:
list_ele = ele_or_page.s_ele().children(timeout=0.1)
except:
list_ele = []
length = len(list_ele)
body_unit = ' ' if last_one else ''
tail = '├───'
new_body = body + body_unit
if length > 0 and layer >= 1:
new_last_one = False
for i in range(length):
if i == length - 1:
tail = '└───'
new_last_one = True
e = list_ele[i]
print(f'{new_body}{tail}{i}<{e.tag}> {e.attrs}')
tree(e, layer - 1, new_last_one, new_body)

View File

@ -50,3 +50,6 @@ def is_cookie_in_driver(page: ChromiumBase, cookie: dict) -> bool: ...
def get_blob(page: ChromiumBase, url: str, as_bytes: bool = True) -> bytes: ... def get_blob(page: ChromiumBase, url: str, as_bytes: bool = True) -> bytes: ...
def tree(ele_or_page, layer=5, last_one=False, body='') -> None: ...

View File

@ -216,8 +216,8 @@ class ChromiumBase(BasePage):
def add_ele(self, def add_ele(self,
outerHTML: str, outerHTML: str,
insert_to: Optional[ChromiumElement, str, Tuple[str, str]] = None, insert_to: Union[ChromiumElement, str, Tuple[str, str], None] = None,
before: Optional[ChromiumElement, str, Tuple[str, str]] = None) -> ChromiumElement: ... before: Union[ChromiumElement, str, Tuple[str, str], None] = None) -> ChromiumElement: ...
def get_frame(self, loc_ind_ele: Union[str, int, tuple, ChromiumFrame], timeout: float = None) -> ChromiumFrame: ... def get_frame(self, loc_ind_ele: Union[str, int, tuple, ChromiumFrame], timeout: float = None) -> ChromiumFrame: ...

View File

@ -6,7 +6,7 @@
@License : BSD 3-Clause. @License : BSD 3-Clause.
""" """
from pathlib import Path from pathlib import Path
from typing import Optional, Union from typing import Union
from .downloader import DownloadMission from .downloader import DownloadMission
from .._elements.chromium_element import ChromiumElement from .._elements.chromium_element import ChromiumElement
@ -16,9 +16,9 @@ class Clicker(object):
def __init__(self, ele: ChromiumElement): def __init__(self, ele: ChromiumElement):
self._ele: ChromiumElement = ... self._ele: ChromiumElement = ...
def __call__(self, by_js: Optional[bool, str] = False, timeout: float = 1.5, wait_stop: bool = True) -> bool: ... def __call__(self, by_js: Union[bool, str, None] = False, timeout: float = 1.5, wait_stop: bool = True) -> bool: ...
def left(self, by_js: Optional[bool, str] = False, timeout: float = 1.5, wait_stop: bool = True) -> bool: ... def left(self, by_js: Union[bool, str, None] = False, timeout: float = 1.5, wait_stop: bool = True) -> bool: ...
def right(self) -> None: ... def right(self) -> None: ...

View File

@ -32,9 +32,9 @@ class DownloadManager(object):
def set_file_exists(self, tab_id: str, mode: Literal['rename', 'skip', 'overwrite']) -> None: ... def set_file_exists(self, tab_id: str, mode: Literal['rename', 'skip', 'overwrite']) -> None: ...
def set_flag(self, tab_id: str, flag: Optional[bool, DownloadMission]) -> None: ... def set_flag(self, tab_id: str, flag: Union[bool, DownloadMission, None]) -> None: ...
def get_flag(self, tab_id: str) -> Optional[bool, DownloadMission]: ... def get_flag(self, tab_id: str) -> Union[bool, DownloadMission, None]: ...
def get_tab_missions(self, tab_id: str) -> list: ... def get_tab_missions(self, tab_id: str) -> list: ...
@ -54,7 +54,7 @@ class DownloadManager(object):
class TabDownloadSettings(object): class TabDownloadSettings(object):
TABS: dict = ... TABS: dict = ...
tab_id: str = ... tab_id: str = ...
waiting_flag: Optional[bool, dict] = ... waiting_flag: Union[bool, dict, None] = ...
rename: Optional[str] = ... rename: Optional[str] = ...
suffix: Optional[str] = ... suffix: Optional[str] = ...
path: Optional[str] = ... path: Optional[str] = ...

View File

@ -6,7 +6,7 @@
@License : BSD 3-Clause. @License : BSD 3-Clause.
""" """
from queue import Queue from queue import Queue
from typing import Union, Dict, List, Iterable, Optional, Literal from typing import Union, Dict, List, Iterable, Optional, Literal, Any
from requests.structures import CaseInsensitiveDict from requests.structures import CaseInsensitiveDict
@ -23,7 +23,7 @@ class Listener(object):
self._page: ChromiumBase = ... self._page: ChromiumBase = ...
self._address: str = ... self._address: str = ...
self._target_id: str = ... self._target_id: str = ...
self._targets: Optional[str, dict] = ... self._targets: Union[str, dict, None] = ...
self._method: set = ... self._method: set = ...
self._res_type: set = ... self._res_type: set = ...
self._caught: Queue = ... self._caught: Queue = ...
@ -39,16 +39,16 @@ class Listener(object):
def targets(self) -> Optional[set]: ... def targets(self) -> Optional[set]: ...
def set_targets(self, def set_targets(self,
targets: Optional[str, list, tuple, set, bool] = True, targets: Union[str, list, tuple, set, bool, None] = True,
is_regex: Optional[bool] = False, is_regex: Optional[bool] = False,
method: Optional[str, list, tuple, set, bool] = ('GET', 'POST'), method: Union[str, list, tuple, set, bool, None] = ('GET', 'POST'),
res_type: Optional[__RES_TYPE__, list, tuple, set, bool] = True) -> None: ... res_type: Union[__RES_TYPE__, list, tuple, set, bool, None] = True) -> None: ...
def start(self, def start(self,
targets: Optional[str, list, tuple, set, bool] = None, targets: Union[str, list, tuple, set, bool, None] = None,
is_regex: Optional[bool] = None, is_regex: Optional[bool] = None,
method: Optional[str, list, tuple, set, bool] = None, method: Union[str, list, tuple, set, bool, None] = None,
res_type: Optional[__RES_TYPE__, list, tuple, set, bool] = None) -> None: ... res_type: Union[__RES_TYPE__, list, tuple, set, bool, None] = None) -> None: ...
def stop(self) -> None: ... def stop(self) -> None: ...
@ -172,7 +172,7 @@ class Request(object):
def headers(self) -> dict: ... def headers(self) -> dict: ...
@property @property
def postData(self) -> Optional[str, dict]: ... def postData(self) -> Any: ...
@property @property
def extra_info(self) -> Optional[RequestExtraInfo]: ... def extra_info(self) -> Optional[RequestExtraInfo]: ...
@ -208,7 +208,7 @@ class Response(object):
self._response: dict = ... self._response: dict = ...
self._raw_body: str = ... self._raw_body: str = ...
self._is_base64_body: bool = ... self._is_base64_body: bool = ...
self._body: Optional[str, dict] = ... self._body: Union[str, dict, None] = ...
self._headers: dict = ... self._headers: dict = ...
@property @property
@ -221,7 +221,7 @@ class Response(object):
def raw_body(self) -> str: ... def raw_body(self) -> str: ...
@property @property
def body(self) -> Optional[str, dict]: ... def body(self) -> Any: ...
class ExtraInfo(object): class ExtraInfo(object):

View File

@ -6,7 +6,7 @@
@License : BSD 3-Clause. @License : BSD 3-Clause.
""" """
from pathlib import Path from pathlib import Path
from typing import Union, Tuple, Literal, Any, Optional from typing import Union, Tuple, Literal, Any
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
@ -64,7 +64,7 @@ class ChromiumBaseSetter(BasePageSetter):
def upload_files(self, files: Union[str, list, tuple]) -> None: ... def upload_files(self, files: Union[str, list, tuple]) -> None: ...
def blocked_urls(self, urls: Optional[list, tuple, str]) -> None: ... def blocked_urls(self, urls: Union[list, tuple, str, None]) -> None: ...
class TabSetter(ChromiumBaseSetter): class TabSetter(ChromiumBaseSetter):
@ -109,7 +109,7 @@ class SessionPageSetter(BasePageSetter):
def timeout(self, second: float) -> None: ... def timeout(self, second: float) -> None: ...
def encoding(self, encoding: Optional[str, None], set_all: bool = True) -> None: ... def encoding(self, encoding: Union[str, None], set_all: bool = True) -> None: ...
def headers(self, headers: dict) -> None: ... def headers(self, headers: dict) -> None: ...