From abd6aa707d17fe1344162045b0020b4d44ca94eb Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 16 May 2022 19:18:08 +0800 Subject: [PATCH] =?UTF-8?q?2.6.0=EF=BC=8C=E6=96=B0=E5=A2=9EListener?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/driver_element.py | 8 +++---- DrissionPage/tools.py | 41 ++++++++++++++++++++++++++-------- docs/index.html | 2 +- docs/版本历史.md | 15 ++++++++----- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index 683dd65..a877e5c 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -285,7 +285,7 @@ class DriverElement(DrissionElement): index: int = 1, filter_loc: Union[tuple, str] = '', timeout: float = None) -> Union['DriverElement', str, None]: - """返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n + """返回当前元素前面的一个元素,可指定筛选条件和第几个。查找范围不限兄弟元,而是整个DOM文档 \n :param index: 前面第几个查询结果元素 :param filter_loc: 用于筛选元素的查询语法 :param timeout: 查找元素的超时时间 @@ -297,7 +297,7 @@ class DriverElement(DrissionElement): index: int = 1, filter_loc: Union[tuple, str] = '', timeout: float = None) -> Union['DriverElement', str, None]: - """返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n + """返回当前元素后面的一个元素,可指定筛选条件和第几个。查找范围不限兄弟元,而是整个DOM文档 \n :param index: 后面第几个查询结果元素 :param filter_loc: 用于筛选元素的查询语法 :param timeout: 查找元素的超时时间 @@ -328,7 +328,7 @@ class DriverElement(DrissionElement): def befores(self, filter_loc: Union[tuple, str] = '', timeout: float = None) -> List[Union['DriverElement', str]]: - """返回后面全部兄弟元素或节点组成的列表,可用查询语法筛选 \n + """返回当前元素后面符合条件的全部兄弟元素或节点组成的列表,可用查询语法筛选。查找范围不限兄弟元,而是整个DOM文档 \n :param filter_loc: 用于筛选元素的查询语法 :param timeout: 查找元素的超时时间 :return: 本元素前面的元素或节点组成的列表 @@ -338,7 +338,7 @@ class DriverElement(DrissionElement): def afters(self, filter_loc: Union[tuple, str] = '', timeout: float = None) -> List[Union['DriverElement', str]]: - """返回前面全部兄弟元素或节点组成的列表,可用查询语法筛选 \n + """返回当前元素前面符合条件的全部兄弟元素或节点组成的列表,可用查询语法筛选。查找范围不限兄弟元,而是整个DOM文档 \n :param filter_loc: 用于筛选元素的查询语法 :param timeout: 查找元素的超时时间 :return: 本元素后面的元素或节点组成的列表 diff --git a/DrissionPage/tools.py b/DrissionPage/tools.py index c4485f3..f8dfbab 100644 --- a/DrissionPage/tools.py +++ b/DrissionPage/tools.py @@ -5,7 +5,7 @@ from json import loads, JSONDecodeError from threading import Thread from time import perf_counter, sleep -from typing import Union, Tuple, List +from typing import Union, Tuple, List, Iterable from pychrome import Tab @@ -15,7 +15,13 @@ from .mix_page import MixPage class ResponseData(object): + """返回的数据包管理类""" + def __init__(self, response: dict, body: str): + """初始化 \n + :param response: response格式化的数据 + :param body: response包含的内容 + """ self.response = response self.raw_body = body self._json_body = None @@ -25,6 +31,7 @@ class ResponseData(object): @property def body(self): + """返回body内容,如果是json格式,自动进行转换,其它格式直接返回文本""" if self._json_body is not False and self.response.get('mimeType', None) == 'application/json': if self._json_body is None: try: @@ -56,13 +63,7 @@ class Listener(object): self._response_count = 0 self._requestIds = {} - - # @property - # def first_result(self) -> Union[ResponseData, None]: - # try: - # return list(self.results.values())[0][0] - # except IndexError: - # return None + self._a_response_loaded = False def set_targets(self, targets: Union[str, List[str], Tuple[str]]) -> None: """设置要拦截的目标,可以设置多个 \n @@ -153,7 +154,7 @@ class Listener(object): self.tab.Network.responseReceived = self._null_function self.tab.Network.loadingFinished = self._null_function - def _loading_finished(self, **kwargs) -> None: + def _loading_finished(self, **kwargs): """请求完成时处理方法""" requestId = kwargs['requestId'] target = self._requestIds.pop(requestId, None) @@ -166,6 +167,28 @@ class Listener(object): self.results[target] = [response] self._response_count += 1 + self._a_response_loaded = True + + def steps(self, response_num: int = 1) -> Iterable: + """用于单步操作,可实现没收到若干个数据包执行一步操作(如翻页) \n + 于是可以根据数据包是否加载完成来决定是否翻页,无须从页面dom去判断是否加载完成 \n + 大大简化代码,提高可靠性 \n + eg: for i in listener.steps(2): \n + btn.click() \n + :param response_num: 每接收到多少个数据包触发 + :return: None + """ + count = 0 + while True: + if not self.listening: + return + + if self._a_response_loaded: + self._a_response_loaded = False + count += 1 + if count % response_num == 0: + yield + def _response_received(self, **kwargs) -> None: """接收到返回信息时处理方法""" for target in self.targets: diff --git a/docs/index.html b/docs/index.html index 23366ce..42f6469 100644 --- a/docs/index.html +++ b/docs/index.html @@ -27,7 +27,7 @@ -
加载中。。。
+
加载中,如加载缓慢请刷新页面。