mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
调整格式
This commit is contained in:
parent
32206e1d0e
commit
08485c05d0
@ -4,6 +4,7 @@
|
|||||||
@Contact : g1879@qq.com
|
@Contact : g1879@qq.com
|
||||||
@File : drission.py
|
@File : drission.py
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
@ -60,10 +61,12 @@ class Drission(object):
|
|||||||
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')
|
||||||
|
|
||||||
if 'chromedriver_path' in om.get_option('paths') and om.get_option('paths')['chromedriver_path']:
|
if 'chromedriver_path' in om.get_option('paths') and om.get_option('paths')['chromedriver_path']:
|
||||||
self._driver_path = om.get_option('paths')['chromedriver_path']
|
self._driver_path = om.get_option('paths')['chromedriver_path']
|
||||||
else:
|
else:
|
||||||
self._driver_options = _chrome_options_to_dict(driver_or_options)
|
self._driver_options = _chrome_options_to_dict(driver_or_options)
|
||||||
|
|
||||||
if 'driver_path' in self._driver_options and self._driver_options['driver_path']:
|
if 'driver_path' in self._driver_options and self._driver_options['driver_path']:
|
||||||
self._driver_path = self._driver_options['driver_path']
|
self._driver_path = self._driver_options['driver_path']
|
||||||
|
|
||||||
@ -150,6 +153,7 @@ class Drission(object):
|
|||||||
self._driver = None
|
self._driver = None
|
||||||
self._driver = self.driver
|
self._driver = self.driver
|
||||||
self._driver.get(url)
|
self._driver.get(url)
|
||||||
|
|
||||||
for cookie in cookies:
|
for cookie in cookies:
|
||||||
self._ensure_add_cookie(cookie)
|
self._ensure_add_cookie(cookie)
|
||||||
|
|
||||||
@ -190,8 +194,10 @@ class Drission(object):
|
|||||||
# 翻译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}
|
||||||
|
|
||||||
if i.expires:
|
if i.expires:
|
||||||
cookie_data['expiry'] = i.expires
|
cookie_data['expiry'] = i.expires
|
||||||
|
|
||||||
self._ensure_add_cookie(cookie_data, driver=driver)
|
self._ensure_add_cookie(cookie_data, driver=driver)
|
||||||
|
|
||||||
def _ensure_add_cookie(self, cookie, override_domain=None, driver=None) -> None:
|
def _ensure_add_cookie(self, cookie, override_domain=None, driver=None) -> None:
|
||||||
@ -202,6 +208,7 @@ class Drission(object):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
driver = driver or self.driver
|
driver = driver or self.driver
|
||||||
|
|
||||||
if override_domain:
|
if override_domain:
|
||||||
cookie['domain'] = override_domain
|
cookie['domain'] = override_domain
|
||||||
|
|
||||||
@ -224,6 +231,7 @@ class Drission(object):
|
|||||||
if not self._is_cookie_in_driver(cookie, driver):
|
if not self._is_cookie_in_driver(cookie, driver):
|
||||||
cookie['domain'] = extract(cookie['domain']).registered_domain
|
cookie['domain'] = extract(cookie['domain']).registered_domain
|
||||||
driver.add_cookie(cookie)
|
driver.add_cookie(cookie)
|
||||||
|
|
||||||
if not self._is_cookie_in_driver(cookie):
|
if not self._is_cookie_in_driver(cookie):
|
||||||
raise WebDriverException(f"Couldn't add the following cookie to the webdriver\n{cookie}\n")
|
raise WebDriverException(f"Couldn't add the following cookie to the webdriver\n{cookie}\n")
|
||||||
|
|
||||||
@ -236,11 +244,13 @@ class Drission(object):
|
|||||||
"""
|
"""
|
||||||
driver = driver or self.driver
|
driver = driver or self.driver
|
||||||
for driver_cookie in driver.get_cookies():
|
for driver_cookie in driver.get_cookies():
|
||||||
|
|
||||||
if (cookie['name'] == driver_cookie['name'] and
|
if (cookie['name'] == driver_cookie['name'] and
|
||||||
cookie['value'] == driver_cookie['value'] and
|
cookie['value'] == driver_cookie['value'] and
|
||||||
(cookie['domain'] == driver_cookie['domain'] or
|
(cookie['domain'] == driver_cookie['domain'] or
|
||||||
f'.{cookie["domain"]}' == driver_cookie['domain'])):
|
f'.{cookie["domain"]}' == driver_cookie['domain'])):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def user_agent_to_session(self, driver: WebDriver = None, session: Session = None) -> None:
|
def user_agent_to_session(self, driver: WebDriver = None, session: Session = None) -> None:
|
||||||
@ -270,10 +280,12 @@ class Drission(object):
|
|||||||
"""关闭session、driver和浏览器"""
|
"""关闭session、driver和浏览器"""
|
||||||
if self._driver:
|
if self._driver:
|
||||||
self.close_driver()
|
self.close_driver()
|
||||||
|
|
||||||
if self._session:
|
if self._session:
|
||||||
self.close_session()
|
self.close_session()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
|
"""关闭对象时关闭浏览器和Session"""
|
||||||
try:
|
try:
|
||||||
self.close()
|
self.close()
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user