From 568a2a34448e2c95dd2127215b3125574823ea66 Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 10 Jan 2023 11:56:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=8A=9F=E8=83=BD=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0browser=5Fpath=E5=8F=82=E6=95=B0=E5=92=8C=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=EF=BC=9Bconnect=5Fchrome()=E6=94=B9=E4=B8=BAconnect?= =?UTF-8?q?=5Fbrowser()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_page.py | 6 +++--- DrissionPage/common.py | 6 +++--- DrissionPage/common.pyi | 2 +- DrissionPage/config.py | 16 +++++++++++----- DrissionPage/config.pyi | 9 +++++++-- DrissionPage/configs.ini | 4 ++-- DrissionPage/drission.py | 4 ++-- DrissionPage/easy_set.py | 5 +++++ DrissionPage/easy_set.pyi | 1 + docs/_sidebar.md | 22 +++++++++++----------- docs/入门指南/基本概念.md | 2 +- docs/入门指南/安装和导入.md | 14 ++++++++++++++ docs/启动配置/使用配置文件.md | 2 +- 13 files changed, 62 insertions(+), 31 deletions(-) diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 8473a43..bd22cfd 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -12,7 +12,7 @@ from requests import Session from .chromium_base import Timeout, ChromiumBase from .chromium_tab import ChromiumTab -from .common import connect_chrome +from .common import connect_browser from .config import DriverOptions from .chromium_driver import ChromiumDriver @@ -47,7 +47,7 @@ class ChromiumPage(ChromiumBase): if addr_driver_opts is None or isinstance(addr_driver_opts, DriverOptions): self.options = addr_driver_opts or DriverOptions() # 从ini文件读取 self.address = self.options.debugger_address - self.process = connect_chrome(self.options)[1] + self.process = connect_browser(self.options)[1] json = self._control_session.get(f'http://{self.address}/json').json() tab_id = [i['id'] for i in json if i['type'] == 'page'][0] @@ -56,7 +56,7 @@ class ChromiumPage(ChromiumBase): self.address = addr_driver_opts self.options = DriverOptions(read_file=False) self.options.debugger_address = addr_driver_opts - self.process = connect_chrome(self.options)[1] + self.process = connect_browser(self.options)[1] if not tab_id: json = self._control_session.get(f'http://{self.address}/json').json() tab_id = [i['id'] for i in json if i['type'] == 'page'][0] diff --git a/DrissionPage/common.py b/DrissionPage/common.py index 5a7692e..1b783b6 100644 --- a/DrissionPage/common.py +++ b/DrissionPage/common.py @@ -514,14 +514,14 @@ def _port_is_using(ip: str, port: str) -> Union[bool, None]: s.close() -def connect_chrome(option): - """连接或启动chrome \n +def connect_browser(option): + """连接或启动浏览器 \n :param option: DriverOptions对象 :return: chrome 路径和进程对象组成的元组 """ system_type = system().lower() debugger_address = option.debugger_address - chrome_path = option.chrome_path + chrome_path = option.browser_path debugger_address = debugger_address[7:] if debugger_address.startswith('http://') else debugger_address ip, port = debugger_address.split(':') diff --git a/DrissionPage/common.pyi b/DrissionPage/common.pyi index 566848c..0c45e31 100644 --- a/DrissionPage/common.pyi +++ b/DrissionPage/common.pyi @@ -53,7 +53,7 @@ def make_absolute_link(link, page: BasePage = None) -> str: ... def is_js_func(func: str) -> bool: ... -def connect_chrome(option: DriverOptions) -> tuple: ... +def connect_browser(option: DriverOptions) -> tuple: ... def location_in_viewport(page, loc_x: int, loc_y: int) -> bool: ... diff --git a/DrissionPage/config.py b/DrissionPage/config.py index cd2ae76..2dd9128 100644 --- a/DrissionPage/config.py +++ b/DrissionPage/config.py @@ -488,9 +488,6 @@ class DriverOptions(Options): self.timeouts['pageLoad'] *= 1000 self.timeouts['script'] *= 1000 - # if '--no-sandbox' not in self._arguments: - # self._arguments.append('--no-sandbox') - @property def driver_path(self): """chromedriver文件路径""" @@ -498,6 +495,11 @@ class DriverOptions(Options): @property def chrome_path(self): + """浏览器启动文件路径""" + return self.browser_path + + @property + def browser_path(self): """浏览器启动文件路径""" return self.binary_location or 'chrome' @@ -718,11 +720,12 @@ class DriverOptions(Options): self.page_load_strategy = value.lower() return self - def set_paths(self, driver_path=None, chrome_path=None, local_port=None, debugger_address=None, download_path=None, - user_data_path=None, cache_path=None): + def set_paths(self, driver_path=None, chrome_path=None, browser_path=None, local_port=None, + debugger_address=None, download_path=None, user_data_path=None, cache_path=None): """快捷的路径设置函数 \n :param driver_path: chromedriver.exe路径 :param chrome_path: chrome.exe路径 + :param browser_path: 浏览器可执行文件路径 :param local_port: 本地端口号 :param debugger_address: 调试浏览器地址,例:127.0.0.1:9222 :param download_path: 下载文件路径 @@ -736,6 +739,9 @@ class DriverOptions(Options): if chrome_path is not None: self.binary_location = str(chrome_path) + if browser_path is not None: + self.binary_location = str(browser_path) + if local_port is not None: self.debugger_address = '' if local_port == '' else f'127.0.0.1:{local_port}' diff --git a/DrissionPage/config.pyi b/DrissionPage/config.pyi index 536c829..871da6b 100644 --- a/DrissionPage/config.pyi +++ b/DrissionPage/config.pyi @@ -5,6 +5,7 @@ """ from configparser import RawConfigParser from http.cookiejar import Cookie +from pathlib import Path from typing import Any, Union, List from requests.cookies import RequestsCookieJar @@ -158,6 +159,9 @@ class DriverOptions(Options): @property def chrome_path(self) -> str: ... + @property + def browser_path(self) -> str: ... + @property def user_data_path(self) -> str: ... @@ -203,8 +207,9 @@ class DriverOptions(Options): def set_page_load_strategy(self, value: str) -> 'DriverOptions': ... def set_paths(self, - driver_path: str = None, - chrome_path: str = None, + driver_path: Union[str, Path] = None, + chrome_path: Union[str, Path] = None, + browser_path: Union[str, Path] = None, local_port: Union[int, str] = None, debugger_address: str = None, download_path: str = None, diff --git a/DrissionPage/configs.ini b/DrissionPage/configs.ini index e92b7ab..763cf8b 100644 --- a/DrissionPage/configs.ini +++ b/DrissionPage/configs.ini @@ -1,10 +1,10 @@ [paths] -chromedriver_path = D:\coding\projects\DrissionPage\DrissionPage\chromedriver.exe +chromedriver_path = tmp_path = [chrome_options] debugger_address = 127.0.0.1:9222 -binary_location = C:\Program Files\Google\Chrome\Application\chrome.exe +binary_location = chrome arguments = ['--no-first-run', '--no-sandbox', '--disable-gpu', '--ignore-certificate-errors', '--disable-infobars', '--disable-popup-blocking'] extensions = [] experimental_options = {'prefs': {'profile.default_content_settings.popups': 0, 'profile.default_content_setting_values': {'notifications': 2}, 'plugins.plugins_list': [{'enabled': False, 'name': 'Chrome PDF Viewer'}]}, 'useAutomationExtension': False, 'excludeSwitches': ['enable-automation']} diff --git a/DrissionPage/drission.py b/DrissionPage/drission.py index a509783..b2cbccb 100644 --- a/DrissionPage/drission.py +++ b/DrissionPage/drission.py @@ -14,7 +14,7 @@ from selenium.webdriver.chrome.options import Options from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from tldextract import extract -from .common import get_pid_from_port, connect_chrome +from .common import get_pid_from_port, connect_browser from .config import SessionOptions, DriverOptions, cookies_to_tuple, session_options_to_dict @@ -103,7 +103,7 @@ class Drission(object): # -----------若指定debug端口且该端口未在使用中,则先启动浏览器进程----------- if self.driver_options.debugger_address: # 启动浏览器进程,同时返回该进程使用的 chrome.exe 路径 - chrome_path, self._debugger = connect_chrome(self.driver_options) + chrome_path, self._debugger = connect_browser(self.driver_options) # -----------创建WebDriver对象----------- self._driver = create_driver(chrome_path, driver_path, self.driver_options) diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index e5d9171..e568256 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -30,6 +30,7 @@ def show_settings(ini_path=None): def set_paths(driver_path=None, chrome_path=None, + browser_path=None, local_port=None, debugger_address=None, tmp_path=None, @@ -41,6 +42,7 @@ def set_paths(driver_path=None, """快捷的路径设置函数 \n :param driver_path: chromedriver.exe路径 :param chrome_path: chrome.exe路径 + :param browser_path: 浏览器可执行文件路径 :param local_port: 本地端口号 :param debugger_address: 调试浏览器地址,例:127.0.0.1:9222 :param download_path: 下载文件路径 @@ -62,6 +64,9 @@ def set_paths(driver_path=None, if chrome_path is not None: om.set_item('chrome_options', 'binary_location', format_path(chrome_path)) + if browser_path is not None: + om.set_item('chrome_options', 'binary_location', format_path(browser_path)) + if local_port is not None: om.set_item('chrome_options', 'debugger_address', format_path(f'127.0.0.1:{local_port}')) diff --git a/DrissionPage/easy_set.pyi b/DrissionPage/easy_set.pyi index 6008faf..04b1ef1 100644 --- a/DrissionPage/easy_set.pyi +++ b/DrissionPage/easy_set.pyi @@ -11,6 +11,7 @@ def show_settings(ini_path: str = None) -> None: ... def set_paths(driver_path: str = None, chrome_path: str = None, + browser_path: str = None, local_port: Union[int, str] = None, debugger_address: str = None, tmp_path: str = None, diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 973e8c8..c9bc7ca 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -1,7 +1,7 @@ * [📣 1 概述](README.md) * [☀️ 2 特性和亮点](#) - + * [💖 2.1 贴心设计](特性和亮点\贴心设计.md) * [🌟 2.2 特性演示](#) * [⭐ 与 requests 对比](特性和亮点\特性演示\与requests代码对比.md) @@ -11,17 +11,17 @@ * [⭐ 下载文件](特性和亮点\特性演示\下载文件.md) * [🧭 3 入门指南](#) - - * [🌏 3.1 基本概念](入门指南\基本概念.md) - * [🌏 3.2 安装和导入](入门指南\安装和导入.md) - * [🌏 3.3 准备工作](入门指南\准备工作.md) - * [🌏 3.4 上手示例](#) + + * [🌏 3.1 安装和导入](入门指南\安装和导入.md) + * [🌏 3.2 准备工作](入门指南\准备工作.md) + * [🌏 3.3 上手示例](#) * [🌐 控制浏览器](入门指南\上手示例\控制浏览器.md) * [🌐 收发数据包](入门指南\上手示例\收发数据包.md) * [🌐 模式切换](入门指南\上手示例\模式切换.md) + * [🌏 3.4 基本概念](入门指南\基本概念.md) * [🛠 4 使用方法](#) - + * [🔨 4.0 简介](WebPage使用方法\3.0简介.md) * [🔨 4.1 创建页面对象](WebPage使用方法\3.1创建页面对象.md) * [🔨 4.2 访问网页](WebPage使用方法\3.2访问网页.md) @@ -35,20 +35,20 @@ * [🔨 4.10 动作链](WebPage使用方法\3.10动作链.md) * [📝 5 启动配置](#) - + * [🗒️ 5.1 概述](启动配置\概述.md) * [🗒️ 5.2 浏览器启动配置](启动配置\浏览器启动配置.md) * [🗒️ 5.3 Session 启动配置](启动配置\Session启动配置.md) * [🗒️ 5.4 使用配置文件](启动配置\使用配置文件.md) * [🧰 6 进阶使用](#) - + * [⚙️ 6.1 打包程序](进阶使用\打包程序.md) * [⚙️ 6.2 监听浏览器网络](进阶使用\监听浏览器网络.md) * [⚙️ 6.3 下载文件](进阶使用\下载文件.md) * [🛠 7 旧版使用方法](#) - + * [🔨 7.0 简介](MixPage使用方法\简介.md) * [🔨 7.1 创建页面对象](MixPage使用方法\创建页面对象.md) * [🔨 7.2 访问网页](MixPage使用方法\访问网页.md) @@ -64,7 +64,7 @@ * [🔨 7.12 DriverPage 和 SessionPage](MixPage使用方法\DriverPage和SessionPage.md) * [⚡️ 8 示例和技巧](#) - + * [🌠 自动登录码云](示例和技巧\自动登录码云.md) * [🌠 采集猫眼电影TOP100榜](示例和技巧\采集猫眼电影TOP100榜.md) * [🌠 下载星巴克产品图片](示例和技巧\下载星巴克产品图片.md) diff --git a/docs/入门指南/基本概念.md b/docs/入门指南/基本概念.md index 4f8a7f9..c518a3b 100644 --- a/docs/入门指南/基本概念.md +++ b/docs/入门指南/基本概念.md @@ -1,6 +1,6 @@ 本节讲解 DrissionPage 的一些基本概念。了解它大概的构成。 -如果您觉得有点懵,可直接跳过本节,从下一节直接上手代码。 +如果您觉得有点懵,可直接跳过本节。 # ✔️ 网页自动化 diff --git a/docs/入门指南/安装和导入.md b/docs/入门指南/安装和导入.md index 8ef6464..09b791a 100644 --- a/docs/入门指南/安装和导入.md +++ b/docs/入门指南/安装和导入.md @@ -1,3 +1,17 @@ +# ✔️运行环境 + +## 📍 操作系统 + +Windows 或 Linux。 + +作者没有 Mac,没测试过,估计是可以的。 + + + +## 📍 python 版本 + +支持 python 3.6 及以上版本。 + # ✔️ 安装 请使用 pip 安装 DrissionPage: diff --git a/docs/启动配置/使用配置文件.md b/docs/启动配置/使用配置文件.md index 76a2ff9..8be6af9 100644 --- a/docs/启动配置/使用配置文件.md +++ b/docs/启动配置/使用配置文件.md @@ -22,7 +22,7 @@ tmp_path = debugger_address = 127.0.0.1:9222 ; 浏览器可执行文件路径 -binary_location = +binary_location = chrome ; 配置信息 arguments = [