mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
调整格式及注释
This commit is contained in:
parent
800d56adf2
commit
7e0f641425
@ -141,9 +141,9 @@ class DriverOptions(Options):
|
|||||||
|
|
||||||
def remove_all_extensions(self):
|
def remove_all_extensions(self):
|
||||||
"""移除所有插件 \n
|
"""移除所有插件 \n
|
||||||
因插件是以整个文件储存,难以移除其中一个,故如须设置则全部移除再重设
|
|
||||||
:return: 当前对象
|
:return: 当前对象
|
||||||
"""
|
"""
|
||||||
|
# 因插件是以整个文件储存,难以移除其中一个,故如须设置则全部移除再重设
|
||||||
self._extensions = []
|
self._extensions = []
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@ -223,16 +223,22 @@ class DriverOptions(Options):
|
|||||||
|
|
||||||
if driver_path is not None:
|
if driver_path is not None:
|
||||||
self._driver_path = format_path(driver_path)
|
self._driver_path = format_path(driver_path)
|
||||||
|
|
||||||
if chrome_path is not None:
|
if chrome_path is not None:
|
||||||
self.binary_location = format_path(chrome_path)
|
self.binary_location = format_path(chrome_path)
|
||||||
|
|
||||||
if debugger_address is not None:
|
if debugger_address is not None:
|
||||||
self.debugger_address = debugger_address
|
self.debugger_address = debugger_address
|
||||||
|
|
||||||
if download_path is not None:
|
if download_path is not None:
|
||||||
self.experimental_options['prefs']['download.default_directory'] = format_path(download_path)
|
self.experimental_options['prefs']['download.default_directory'] = format_path(download_path)
|
||||||
|
|
||||||
if user_data_path is not None:
|
if user_data_path is not None:
|
||||||
self.set_argument('--user-data-dir', format_path(user_data_path))
|
self.set_argument('--user-data-dir', format_path(user_data_path))
|
||||||
|
|
||||||
if cache_path is not None:
|
if cache_path is not None:
|
||||||
self.set_argument('--disk-cache-dir', format_path(cache_path))
|
self.set_argument('--disk-cache-dir', format_path(cache_path))
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
@ -242,32 +248,39 @@ def _dict_to_chrome_options(options: dict) -> Options:
|
|||||||
:return: 保存浏览器配置的ChromeOptions对象
|
:return: 保存浏览器配置的ChromeOptions对象
|
||||||
"""
|
"""
|
||||||
chrome_options = webdriver.ChromeOptions()
|
chrome_options = webdriver.ChromeOptions()
|
||||||
|
# 已打开的浏览器路径
|
||||||
if 'debugger_address' in options and options['debugger_address']:
|
if 'debugger_address' in options and options['debugger_address']:
|
||||||
# 控制已打开的浏览器
|
|
||||||
chrome_options.debugger_address = options['debugger_address']
|
chrome_options.debugger_address = options['debugger_address']
|
||||||
|
|
||||||
|
# 创建新的浏览器
|
||||||
else:
|
else:
|
||||||
|
# 浏览器的exe文件路径
|
||||||
if 'binary_location' in options and options['binary_location']:
|
if 'binary_location' in options and options['binary_location']:
|
||||||
# 手动指定使用的浏览器位置
|
|
||||||
chrome_options.binary_location = options['binary_location']
|
chrome_options.binary_location = options['binary_location']
|
||||||
if 'arguments' in options:
|
|
||||||
# 启动参数
|
# 启动参数
|
||||||
|
if 'arguments' in options:
|
||||||
if not isinstance(options['arguments'], list):
|
if not isinstance(options['arguments'], list):
|
||||||
raise Exception(f'Arguments need list,not {type(options["arguments"])}.')
|
raise Exception(f'Arguments need list,not {type(options["arguments"])}.')
|
||||||
for arg in options['arguments']:
|
for arg in options['arguments']:
|
||||||
chrome_options.add_argument(arg)
|
chrome_options.add_argument(arg)
|
||||||
if 'extension_files' in options and options['extension_files']:
|
|
||||||
# 加载插件
|
# 加载插件
|
||||||
|
if 'extension_files' in options and options['extension_files']:
|
||||||
if not isinstance(options['extension_files'], list):
|
if not isinstance(options['extension_files'], list):
|
||||||
raise Exception(f'Extension files need list,not {type(options["extension_files"])}.')
|
raise Exception(f'Extension files need list,not {type(options["extension_files"])}.')
|
||||||
for arg in options['extension_files']:
|
for arg in options['extension_files']:
|
||||||
chrome_options.add_extension(arg)
|
chrome_options.add_extension(arg)
|
||||||
|
|
||||||
|
# 扩展设置
|
||||||
if 'extensions' in options and options['extensions']:
|
if 'extensions' in options and options['extensions']:
|
||||||
if not isinstance(options['extensions'], list):
|
if not isinstance(options['extensions'], list):
|
||||||
raise Exception(f'Extensions need list,not {type(options["extensions"])}.')
|
raise Exception(f'Extensions need list,not {type(options["extensions"])}.')
|
||||||
for arg in options['extensions']:
|
for arg in options['extensions']:
|
||||||
chrome_options.add_encoded_extension(arg)
|
chrome_options.add_encoded_extension(arg)
|
||||||
if 'experimental_options' in options and options['experimental_options']:
|
|
||||||
# 实验性质的设置参数
|
# 实验性质的设置参数
|
||||||
|
if 'experimental_options' in options and options['experimental_options']:
|
||||||
if not isinstance(options['experimental_options'], dict):
|
if not isinstance(options['experimental_options'], dict):
|
||||||
raise Exception(f'Experimental options need dict,not {type(options["experimental_options"])}.')
|
raise Exception(f'Experimental options need dict,not {type(options["experimental_options"])}.')
|
||||||
for i in options['experimental_options']:
|
for i in options['experimental_options']:
|
||||||
@ -292,6 +305,7 @@ def _chrome_options_to_dict(options: Union[dict, DriverOptions, None]) -> Union[
|
|||||||
re_dict['arguments'] = options.arguments
|
re_dict['arguments'] = options.arguments
|
||||||
re_dict['extensions'] = options.extensions
|
re_dict['extensions'] = options.extensions
|
||||||
re_dict['experimental_options'] = options.experimental_options
|
re_dict['experimental_options'] = options.experimental_options
|
||||||
|
|
||||||
try:
|
try:
|
||||||
re_dict['driver_path'] = options.driver_path
|
re_dict['driver_path'] = options.driver_path
|
||||||
except:
|
except:
|
||||||
|
@ -14,20 +14,20 @@ from selenium.webdriver.chrome.options import Options
|
|||||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
from selenium.webdriver.chrome.webdriver import WebDriver
|
||||||
from tldextract import extract
|
from tldextract import extract
|
||||||
|
|
||||||
from .config import _dict_to_chrome_options, OptionsManager, _chrome_options_to_dict
|
from .config import OptionsManager, _dict_to_chrome_options, _chrome_options_to_dict
|
||||||
|
|
||||||
|
|
||||||
class Drission(object):
|
class Drission(object):
|
||||||
"""Drission类整合了WebDriver对象和HTLSession对象,可按要求创建、关闭及同步cookies"""
|
"""Drission类用于管理WebDriver对象和Session对象,是驱动器的角色"""
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
driver_or_options: Union[WebDriver, dict, Options] = None,
|
driver_or_options: Union[WebDriver, dict, Options] = None,
|
||||||
session_or_options: Union[Session, dict] = None,
|
session_or_options: Union[Session, dict] = None,
|
||||||
ini_path: str = None,
|
ini_path: str = None,
|
||||||
proxy: dict = None):
|
proxy: dict = None):
|
||||||
"""初始化,可接收现成的WebDriver和Session对象,或接收它们的配置信息 \n
|
"""初始化,可接收现成的WebDriver和Session对象,或接收它们的配置信息生成对象 \n
|
||||||
:param driver_or_options: driver对象或chrome设置,Options类或设置字典
|
:param driver_or_options: driver对象或chrome设置,Options类或设置字典
|
||||||
:param session_or_options: Session对象设置
|
:param session_or_options: Session对象或设置
|
||||||
:param ini_path: ini文件路径
|
:param ini_path: ini文件路径
|
||||||
:param proxy: 代理设置
|
:param proxy: 代理设置
|
||||||
"""
|
"""
|
||||||
@ -35,16 +35,28 @@ class Drission(object):
|
|||||||
self._driver = None
|
self._driver = None
|
||||||
self._driver_path = 'chromedriver'
|
self._driver_path = 'chromedriver'
|
||||||
self._proxy = proxy
|
self._proxy = proxy
|
||||||
|
|
||||||
|
# 若接收到Session对象,直接记录
|
||||||
if isinstance(session_or_options, Session):
|
if isinstance(session_or_options, Session):
|
||||||
self._session = session_or_options
|
self._session = session_or_options
|
||||||
|
|
||||||
|
# 否则记录其配置信息
|
||||||
else:
|
else:
|
||||||
|
|
||||||
|
# 若接收到配置信息则记录,否则从ini文件读取
|
||||||
if session_or_options is None:
|
if session_or_options is None:
|
||||||
self._session_options = OptionsManager(ini_path).get_option('session_options')
|
self._session_options = OptionsManager(ini_path).get_option('session_options')
|
||||||
else:
|
else:
|
||||||
self._session_options = session_or_options
|
self._session_options = session_or_options
|
||||||
|
|
||||||
|
# 若接收到WebDriver对象,直接记录
|
||||||
if isinstance(driver_or_options, WebDriver):
|
if isinstance(driver_or_options, WebDriver):
|
||||||
self._driver = driver_or_options
|
self._driver = driver_or_options
|
||||||
|
|
||||||
|
# 否则记录其配置信息
|
||||||
else:
|
else:
|
||||||
|
|
||||||
|
# 若接收到配置信息则记录,否则从ini文件读取
|
||||||
if driver_or_options is None:
|
if driver_or_options is None:
|
||||||
om = OptionsManager(ini_path)
|
om = OptionsManager(ini_path)
|
||||||
self._driver_options = om.get_option('chrome_options')
|
self._driver_options = om.get_option('chrome_options')
|
||||||
@ -57,14 +69,16 @@ class Drission(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def session(self) -> Session:
|
def session(self) -> Session:
|
||||||
"""返回Session对象,如为None则按配置信息创建"""
|
"""返回Session对象,如未初始化则按配置信息创建"""
|
||||||
if self._session is None:
|
if self._session is None:
|
||||||
self._session = Session()
|
self._session = Session()
|
||||||
attrs = ['headers', 'cookies', 'auth', 'proxies', 'hooks', 'params', 'verify',
|
attrs = ['headers', 'cookies', 'auth', 'proxies', 'hooks', 'params', 'verify',
|
||||||
'cert', 'adapters', 'stream', 'trust_env', 'max_redirects']
|
'cert', 'adapters', 'stream', 'trust_env', 'max_redirects']
|
||||||
|
|
||||||
for i in attrs:
|
for i in attrs:
|
||||||
if i in self._session_options:
|
if i in self._session_options:
|
||||||
exec(f'self._session.{i} = self._session_options["{i}"]')
|
exec(f'self._session.{i} = self._session_options["{i}"]')
|
||||||
|
|
||||||
if self._proxy:
|
if self._proxy:
|
||||||
self._session.proxies = self._proxy
|
self._session.proxies = self._proxy
|
||||||
|
|
||||||
@ -72,7 +86,7 @@ class Drission(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def driver(self) -> WebDriver:
|
def driver(self) -> WebDriver:
|
||||||
"""返回WebDriver对象,如为None则按配置信息创建"""
|
"""返回WebDriver对象,如未初始化则按配置信息创建"""
|
||||||
if self._driver is None:
|
if self._driver is None:
|
||||||
if isinstance(self._driver_options, dict):
|
if isinstance(self._driver_options, dict):
|
||||||
options = _dict_to_chrome_options(self._driver_options)
|
options = _dict_to_chrome_options(self._driver_options)
|
||||||
@ -125,8 +139,10 @@ class Drission(object):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
self._proxy = proxies
|
self._proxy = proxies
|
||||||
|
|
||||||
if self._session:
|
if self._session:
|
||||||
self._session.proxies = proxies
|
self._session.proxies = proxies
|
||||||
|
|
||||||
if self._driver:
|
if self._driver:
|
||||||
cookies = self._driver.get_cookies()
|
cookies = self._driver.get_cookies()
|
||||||
url = self._driver.current_url
|
url = self._driver.current_url
|
||||||
@ -148,8 +164,10 @@ class Drission(object):
|
|||||||
"""
|
"""
|
||||||
driver = driver or self.driver
|
driver = driver or self.driver
|
||||||
session = session or self.session
|
session = session or self.session
|
||||||
|
|
||||||
if copy_user_agent:
|
if copy_user_agent:
|
||||||
self.user_agent_to_session(driver, session)
|
self.user_agent_to_session(driver, session)
|
||||||
|
|
||||||
for cookie in driver.get_cookies():
|
for cookie in driver.get_cookies():
|
||||||
session.cookies.set(cookie['name'], cookie['value'], domain=cookie['domain'])
|
session.cookies.set(cookie['name'], cookie['value'], domain=cookie['domain'])
|
||||||
|
|
||||||
@ -165,8 +183,10 @@ class Drission(object):
|
|||||||
driver = driver or self.driver
|
driver = driver or self.driver
|
||||||
session = session or self.session
|
session = session or self.session
|
||||||
domain = urlparse(url).netloc
|
domain = urlparse(url).netloc
|
||||||
|
|
||||||
if not domain:
|
if not domain:
|
||||||
raise Exception('Without specifying a domain')
|
raise Exception('Without specifying a domain')
|
||||||
|
|
||||||
# 翻译cookies
|
# 翻译cookies
|
||||||
for i in [x for x in session.cookies if domain in x.domain]:
|
for i in [x for x in session.cookies if domain in x.domain]:
|
||||||
cookie_data = {'name': i.name, 'value': str(i.value), 'path': i.path, 'domain': i.domain}
|
cookie_data = {'name': i.name, 'value': str(i.value), 'path': i.path, 'domain': i.domain}
|
||||||
@ -186,12 +206,15 @@ class Drission(object):
|
|||||||
cookie['domain'] = override_domain
|
cookie['domain'] = override_domain
|
||||||
|
|
||||||
cookie_domain = cookie['domain'] if cookie['domain'][0] != '.' else cookie['domain'][1:]
|
cookie_domain = cookie['domain'] if cookie['domain'][0] != '.' else cookie['domain'][1:]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
browser_domain = extract(driver.current_url).fqdn
|
browser_domain = extract(driver.current_url).fqdn
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
browser_domain = ''
|
browser_domain = ''
|
||||||
|
|
||||||
if cookie_domain not in browser_domain:
|
if cookie_domain not in browser_domain:
|
||||||
driver.get(f'http://{cookie_domain.lstrip("http://")}')
|
driver.get(f'http://{cookie_domain.lstrip("http://")}')
|
||||||
|
|
||||||
if 'expiry' in cookie:
|
if 'expiry' in cookie:
|
||||||
cookie['expiry'] = int(cookie['expiry'])
|
cookie['expiry'] = int(cookie['expiry'])
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user