From 4db2f71d1574254fa453a2f3fc65ccc44bf4eae7 Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 19 Jan 2024 16:04:41 +0800 Subject: [PATCH] =?UTF-8?q?4.0.3.3(+)=20=E4=BF=AE=E5=A4=8Dset=5Fbrowser=5F?= =?UTF-8?q?path()=E5=AF=BC=E8=87=B4auto=5Fport()=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9B=20=E9=87=8D=E6=9E=84wait=5Funtil()?= =?UTF-8?q?=EF=BC=9B=20ini=E6=96=87=E4=BB=B6=E5=A2=9E=E5=8A=A0'--disable-f?= =?UTF-8?q?eatures=3DPrivacySandboxSettings4'=EF=BC=9B=20get=5Fblob()?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=94=B9=E4=B8=BAas=5Fbytes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 2 +- DrissionPage/_configs/chromium_options.py | 1 - DrissionPage/_configs/configs.ini | 2 +- DrissionPage/_functions/tools.py | 41 ++++++----------------- DrissionPage/_functions/tools.pyi | 2 +- DrissionPage/_functions/web.py | 10 ++++-- DrissionPage/_functions/web.pyi | 2 +- README.md | 2 +- 8 files changed, 24 insertions(+), 38 deletions(-) diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 25f0e09..ebceae1 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] -__version__ = '4.0.3.2' +__version__ = '4.0.3.3' diff --git a/DrissionPage/_configs/chromium_options.py b/DrissionPage/_configs/chromium_options.py index 11b56f2..d9ed2d7 100644 --- a/DrissionPage/_configs/chromium_options.py +++ b/DrissionPage/_configs/chromium_options.py @@ -448,7 +448,6 @@ class ChromiumOptions(object): :return: 当前对象 """ self._browser_path = str(path) - self._auto_port = False return self def set_download_path(self, path): diff --git a/DrissionPage/_configs/configs.ini b/DrissionPage/_configs/configs.ini index 3872ad9..f2de400 100644 --- a/DrissionPage/_configs/configs.ini +++ b/DrissionPage/_configs/configs.ini @@ -5,7 +5,7 @@ tmp_path = [chromium_options] address = 127.0.0.1:9222 browser_path = chrome -arguments = ['--no-default-browser-check', '--disable-suggestions-ui', '--no-first-run', '--disable-infobars', '--disable-popup-blocking', '--hide-crash-restore-bubble'] +arguments = ['--no-default-browser-check', '--disable-suggestions-ui', '--no-first-run', '--disable-infobars', '--disable-popup-blocking', '--hide-crash-restore-bubble', '--disable-features=PrivacySandboxSettings4'] extensions = [] prefs = {'profile.default_content_settings.popups': 0, 'profile.default_content_setting_values': {'notifications': 2}} flags = {} diff --git a/DrissionPage/_functions/tools.py b/DrissionPage/_functions/tools.py index 3e7f8ff..5f6b3bf 100644 --- a/DrissionPage/_functions/tools.py +++ b/DrissionPage/_functions/tools.py @@ -122,40 +122,21 @@ def get_chrome_hwnds_from_pid(pid, title): return hwnds -def wait_until(page, condition, timeout=10, poll=0.1, raise_err=True): - """等待返回值不为False或空,直到超时 - :param page: DrissionPage对象 - :param condition: 等待条件,返回值不为False则停止等待 +def wait_until(function, kwargs=None, timeout=10): + """等待传入的方法返回值不为假 + :param function: 要执行的方法 + :param kwargs: 方法参数 :param timeout: 超时时间(秒) - :param poll: 轮询间隔 - :param raise_err: 是否抛出异常 - :return: DP Element or bool + :return: 执行结果,超时抛出TimeoutError """ + if kwargs is None: + kwargs = {} end_time = perf_counter() + timeout - if isinstance(condition, str) or isinstance(condition, tuple): - if not callable(getattr(page, 's_ele', None)): - raise AttributeError('page对象缺少s_ele方法') - condition_method = lambda page: page.s_ele(condition) - elif callable(condition): - condition_method = condition - else: - raise ValueError('condition必须是函数或者字符串或者元组') while perf_counter() < end_time: - try: - value = condition_method(page) - if value: - return value - except Exception: - pass - - sleep(poll) - if perf_counter() > end_time: - break - - if raise_err: - raise TimeoutError(f'等待超时(等待{timeout}秒)。') - else: - return False + value = function(**kwargs) + if value: + return value + raise TimeoutError def stop_process_on_port(port): diff --git a/DrissionPage/_functions/tools.pyi b/DrissionPage/_functions/tools.pyi index 057a43f..8e0ae9b 100644 --- a/DrissionPage/_functions/tools.pyi +++ b/DrissionPage/_functions/tools.pyi @@ -28,7 +28,7 @@ def get_browser_progress_id(progress: Union[popen, None], address: str) -> Union def get_chrome_hwnds_from_pid(pid: Union[str, int], title: str) -> list: ... -def wait_until(page, condition: Union[FunctionType, str, tuple], timeout: float, poll: float, raise_err: bool): ... +def wait_until(function: callable, kwargs: dict = None, timeout: float = 10): ... def stop_process_on_port(port: Union[int, str]) -> None: ... diff --git a/DrissionPage/_functions/web.py b/DrissionPage/_functions/web.py index f731162..c34c778 100644 --- a/DrissionPage/_functions/web.py +++ b/DrissionPage/_functions/web.py @@ -330,7 +330,13 @@ def is_cookie_in_driver(page, cookie): return False -def get_blob(page, url, base64_to_bytes=True): +def get_blob(page, url, as_bytes=True): + """获取知道blob资源 + :param page: 资源所在页面对象 + :param url: 资源url + :param as_bytes: 是否以字节形式返回 + :return: 资源内容 + """ if not url.startswith('blob'): raise TypeError('该链接非blob类型。') js = """ @@ -352,6 +358,6 @@ def get_blob(page, url, base64_to_bytes=True): result = page.run_js(js, url) except: raise RuntimeError('无法获取该资源。') - if base64_to_bytes: + if as_bytes: from base64 import b64decode return b64decode(result.split(',', 1)[-1]) diff --git a/DrissionPage/_functions/web.pyi b/DrissionPage/_functions/web.pyi index e86b124..7b5daef 100644 --- a/DrissionPage/_functions/web.pyi +++ b/DrissionPage/_functions/web.pyi @@ -49,4 +49,4 @@ def set_browser_cookies(page: ChromiumBase, cookies: Union[RequestsCookieJar, li def is_cookie_in_driver(page: ChromiumBase, cookie: dict) -> bool: ... -def get_blob(page: ChromiumBase, url: str, base64_to_bytes: bool = True) -> bytes: ... +def get_blob(page: ChromiumBase, url: str, as_bytes: bool = True) -> bytes: ... diff --git a/README.md b/README.md index e2533f9..db38f30 100644 --- a/README.md +++ b/README.md @@ -123,4 +123,4 @@ python 版本:3.6 及以上 如果本项目对您有所帮助,不妨请作者我喝杯咖啡 :) -![](http://g1879.gitee.io/drissionpagedocs/imgs/code.jpg) +![](https://gitee.com/g1879/DrissionPageDocs/raw/master/docs/imgs/code.jpg)