创建driver时可自动下载chromedriver.exe

This commit is contained in:
g1879 2020-12-24 17:08:38 +08:00
parent f9ba0625e3
commit d10da8d718
3 changed files with 60 additions and 30 deletions

View File

@ -22,7 +22,7 @@ class OptionsManager(object):
"""初始化,读取配置文件,如没有设置临时文件夹,则设置并新建 \n """初始化,读取配置文件,如没有设置临时文件夹,则设置并新建 \n
:param path: ini文件的路径默认读取模块文件夹下的 :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 = RawConfigParser()
self._conf.read(self.ini_path, encoding='utf-8') self._conf.read(self.ini_path, encoding='utf-8')

View File

@ -92,19 +92,32 @@ class Drission(object):
options.add_argument(f'--proxy-server={self._proxy["http"]}') options.add_argument(f'--proxy-server={self._proxy["http"]}')
driver_path = self._driver_options.get('driver_path', None) or 'chromedriver' 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: 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) self._driver = webdriver.Chrome(driver_path, options=options)
except (WebDriverException, SessionNotCreatedException): except (WebDriverException, SessionNotCreatedException):
print('未指定chromedriver路径或版本与Chrome不匹配可执行easy_set.get_match_driver()自动下载匹配的版本。') from .easy_set import get_match_driver
exit(0)
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", { self._driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {

View File

@ -190,27 +190,33 @@ def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bo
# -------------------------自动识别chrome版本号并下载对应driver------------------------ # -------------------------自动识别chrome版本号并下载对应driver------------------------
def get_match_driver(ini_path: str = None, def get_match_driver(ini_path: Union[str, None] = 'default',
save_path: str = None, 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 """自动识别chrome版本并下载匹配的driver \n
:param ini_path: 要读取和修改的ini文件路径 :param ini_path: 要读取和修改的ini文件路径
:param save_path: chromedriver保存路径 :param save_path: chromedriver保存路径
:param chrome_path: 指定chrome.exe位置 :param chrome_path: 指定chrome.exe位置
:param show_msg: 是否打印信息
:param check_version: 是否检查版本匹配
:return: None :return: None
""" """
save_path = save_path or str(Path(__file__).parent) 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 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) 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。') print('没有找到对应版本的driver。')
try: try:
@ -218,28 +224,37 @@ def get_match_driver(ini_path: str = None,
except TypeError: except TypeError:
driver_path = None driver_path = None
print('\n解压路径', driver_path, '\n') if show_msg:
print('\n解压路径', driver_path, '\n')
if driver_path: if driver_path:
Path(zip_path).unlink() 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): if check_version:
print('获取失败,请手动配置。') if not check_driver_version(driver_path, chrome_path) and show_msg:
print('获取失败,请手动配置。')
else: 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 """从ini文件或系统变量中获取chrome.exe的路径 \n
:param ini_path: ini文件路径 :param ini_path: ini文件路径
:return: chrome.exe路径 :return: chrome.exe路径
""" """
# -----------从ini文件中获取-------------- # -----------从ini文件中获取--------------
try: if ini_path:
path = OptionsManager(ini_path).chrome_options['binary_location'] try:
except KeyError: path = OptionsManager(ini_path).chrome_options['binary_location']
return None except KeyError:
path = None
else:
path = None
if path and Path(path).is_file(): if path and Path(path).is_file():
print('ini文件中', end='') 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' path = Path(r.group(0)) if 'chrome.exe' in r.group(0) else Path(r.group(0)) / 'chrome.exe'
if path.exists(): if path.exists():
print('系统中', end='') if show_msg:
print('系统中', end='')
return str(path) return str(path)
paths = paths.split(';') paths = paths.split(';')
@ -262,7 +278,8 @@ def _get_chrome_path(ini_path: str = None) -> Union[str, None]:
path = Path(path) / 'chrome.exe' path = Path(path) / 'chrome.exe'
if path.exists(): if path.exists():
print('系统中', end='') if show_msg:
print('系统中', end='')
return str(path) return str(path)
@ -283,7 +300,7 @@ def _get_chrome_version(path: str) -> Union[str, None]:
return 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 """根据传入的版本号到镜像网站查找,下载最相近的 \n
:param version: 本地版本号 :param version: 本地版本号
:return: 保存地址 :return: 保存地址
@ -317,7 +334,7 @@ def _download_driver(version: str, save_path: str = None) -> Union[str, None]:
if remote_ver: if remote_ver:
url = f'https://cdn.npm.taobao.org/dist/chromedriver/{remote_ver}chromedriver_win32.zip' url = f'https://cdn.npm.taobao.org/dist/chromedriver/{remote_ver}chromedriver_win32.zip'
save_path = save_path or Path(__file__).parent 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]: if result[0]:
return result[1] return result[1]