From d10da8d71890bbfe4f11ab95f401dbdd5d63bedd Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 24 Dec 2020 17:08:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BAdriver=E6=97=B6=E5=8F=AF?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=B8=8B=E8=BD=BDchromedriver.exe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/config.py | 2 +- DrissionPage/drission.py | 29 ++++++++++++++------ DrissionPage/easy_set.py | 59 ++++++++++++++++++++++++++-------------- 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/DrissionPage/config.py b/DrissionPage/config.py index 17fccd0..a3e5253 100644 --- a/DrissionPage/config.py +++ b/DrissionPage/config.py @@ -22,7 +22,7 @@ class OptionsManager(object): """初始化,读取配置文件,如没有设置临时文件夹,则设置并新建 \n :param path: ini文件的路径,默认读取模块文件夹下的 """ - self.ini_path = path or str(Path(__file__).parent / 'configs.ini') + self.ini_path = str(Path(__file__).parent / 'configs.ini') if path == 'default' or path is None else path self._conf = RawConfigParser() self._conf.read(self.ini_path, encoding='utf-8') diff --git a/DrissionPage/drission.py b/DrissionPage/drission.py index 6930023..88dde3e 100644 --- a/DrissionPage/drission.py +++ b/DrissionPage/drission.py @@ -92,19 +92,32 @@ class Drission(object): options.add_argument(f'--proxy-server={self._proxy["http"]}') driver_path = self._driver_options.get('driver_path', None) or 'chromedriver' + chrome_path = self._driver_options.get('binary_location', None) or 'chrome.exe' + + if options.debugger_address and _check_port(options.debugger_address) is False: + from subprocess import Popen + port = options.debugger_address.split(':')[-1] + + Popen(f'{chrome_path} --remote-debugging-port={port}', shell=False) try: - if options.debugger_address and _check_port(options.debugger_address) is False: - from subprocess import Popen - port = options.debugger_address.split(':')[-1] - chrome_path = self._driver_options.get('binary_location', None) or 'chrome.exe' - Popen(f'{chrome_path} --remote-debugging-port={port}', shell=False) - self._driver = webdriver.Chrome(driver_path, options=options) except (WebDriverException, SessionNotCreatedException): - print('未指定chromedriver路径或版本与Chrome不匹配,可执行easy_set.get_match_driver()自动下载匹配的版本。') - exit(0) + from .easy_set import get_match_driver + + chrome_path = self._driver_options.get('binary_location', None) or None + driver_path = get_match_driver(chrome_path=chrome_path, check_version=False, show_msg=False) + + if driver_path: + try: + self._driver = webdriver.Chrome(driver_path, options=options) + except: + print('无法启动,请检查chromedriver版本与Chrome是否匹配,并手动设置。') + exit(0) + else: + print('无法启动,请检查chromedriver版本与Chrome是否匹配,并手动设置。') + exit(0) # 反爬设置,似乎没用 self._driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index 59e5c52..a64fadb 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -190,27 +190,33 @@ def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bo # -------------------------自动识别chrome版本号并下载对应driver------------------------ -def get_match_driver(ini_path: str = None, +def get_match_driver(ini_path: Union[str, None] = 'default', save_path: str = None, - chrome_path: str = None) -> None: + chrome_path: str = None, + show_msg: bool = True, + check_version: bool = True) -> Union[str, None]: """自动识别chrome版本并下载匹配的driver \n :param ini_path: 要读取和修改的ini文件路径 :param save_path: chromedriver保存路径 :param chrome_path: 指定chrome.exe位置 + :param show_msg: 是否打印信息 + :param check_version: 是否检查版本匹配 :return: None """ save_path = save_path or str(Path(__file__).parent) - chrome_path = chrome_path or _get_chrome_path(ini_path) + chrome_path = chrome_path or _get_chrome_path(ini_path, show_msg) chrome_path = Path(chrome_path).absolute() if chrome_path else None - print('chrome.exe路径', chrome_path, '\n') + if show_msg: + print('chrome.exe路径', chrome_path, '\n') ver = _get_chrome_version(chrome_path) - print('version', ver, '\n') + if show_msg: + print('version', ver, '\n') - zip_path = _download_driver(ver, save_path) + zip_path = _download_driver(ver, save_path, show_msg=show_msg) - if not zip_path: + if not zip_path and show_msg: print('没有找到对应版本的driver。') try: @@ -218,28 +224,37 @@ def get_match_driver(ini_path: str = None, except TypeError: driver_path = None - print('\n解压路径', driver_path, '\n') + if show_msg: + print('\n解压路径', driver_path, '\n') if driver_path: Path(zip_path).unlink() - set_paths(driver_path=driver_path, chrome_path=str(chrome_path), ini_path=ini_path, check_version=False) + if ini_path: + set_paths(driver_path=driver_path, chrome_path=str(chrome_path), ini_path=ini_path, check_version=False) - if not check_driver_version(driver_path, chrome_path): - print('获取失败,请手动配置。') + if check_version: + if not check_driver_version(driver_path, chrome_path) and show_msg: + print('获取失败,请手动配置。') else: - print('获取失败,请手动配置。') + if show_msg: + print('获取失败,请手动配置。') + + return driver_path -def _get_chrome_path(ini_path: str = None) -> Union[str, None]: +def _get_chrome_path(ini_path: str = None, show_msg: bool = True) -> Union[str, None]: """从ini文件或系统变量中获取chrome.exe的路径 \n :param ini_path: ini文件路径 :return: chrome.exe路径 """ # -----------从ini文件中获取-------------- - try: - path = OptionsManager(ini_path).chrome_options['binary_location'] - except KeyError: - return None + if ini_path: + try: + path = OptionsManager(ini_path).chrome_options['binary_location'] + except KeyError: + path = None + else: + path = None if path and Path(path).is_file(): print('ini文件中', end='') @@ -253,7 +268,8 @@ def _get_chrome_path(ini_path: str = None) -> Union[str, None]: path = Path(r.group(0)) if 'chrome.exe' in r.group(0) else Path(r.group(0)) / 'chrome.exe' if path.exists(): - print('系统中', end='') + if show_msg: + print('系统中', end='') return str(path) paths = paths.split(';') @@ -262,7 +278,8 @@ def _get_chrome_path(ini_path: str = None) -> Union[str, None]: path = Path(path) / 'chrome.exe' if path.exists(): - print('系统中', end='') + if show_msg: + print('系统中', end='') return str(path) @@ -283,7 +300,7 @@ def _get_chrome_version(path: str) -> Union[str, None]: return None -def _download_driver(version: str, save_path: str = None) -> Union[str, None]: +def _download_driver(version: str, save_path: str = None, show_msg: bool = True) -> Union[str, None]: """根据传入的版本号到镜像网站查找,下载最相近的 \n :param version: 本地版本号 :return: 保存地址 @@ -317,7 +334,7 @@ def _download_driver(version: str, save_path: str = None) -> Union[str, None]: if remote_ver: url = f'https://cdn.npm.taobao.org/dist/chromedriver/{remote_ver}chromedriver_win32.zip' save_path = save_path or Path(__file__).parent - result = page.download(url, save_path, file_exists='overwrite', show_msg=True) + result = page.download(url, save_path, file_exists='overwrite', show_msg=show_msg) if result[0]: return result[1]