mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
4.0.4.12优化set.cookies()逻辑
This commit is contained in:
parent
4a71435c4b
commit
991baf18d1
@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions
|
|||||||
from ._configs.session_options import SessionOptions
|
from ._configs.session_options import SessionOptions
|
||||||
|
|
||||||
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
|
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
|
||||||
__version__ = '4.0.4.11'
|
__version__ = '4.0.4.12'
|
||||||
|
@ -181,17 +181,14 @@ def cookie_to_dict(cookie):
|
|||||||
cookie_dict = cookie
|
cookie_dict = cookie
|
||||||
|
|
||||||
elif isinstance(cookie, str):
|
elif isinstance(cookie, str):
|
||||||
cookie = cookie.rstrip(';,').split(',' if ',' in cookie else ';')
|
|
||||||
cookie_dict = {}
|
cookie_dict = {}
|
||||||
|
for attr in cookie.strip().rstrip(';,').split(',' if ',' in cookie else ';'):
|
||||||
for key, attr in enumerate(cookie):
|
attr_val = attr.strip().split('=', 1)
|
||||||
attr_val = attr.lstrip().split('=', 1)
|
if attr_val[0] in ('domain', 'path', 'expires', 'max-age', 'HttpOnly', 'secure', 'expiry', 'name', 'value'):
|
||||||
|
cookie_dict[attr_val[0]] = attr_val[1] if len(attr_val) == 2 else ''
|
||||||
if key == 0:
|
else:
|
||||||
cookie_dict['name'] = attr_val[0]
|
cookie_dict['name'] = attr_val[0]
|
||||||
cookie_dict['value'] = attr_val[1] if len(attr_val) == 2 else ''
|
cookie_dict['value'] = attr_val[1] if len(attr_val) == 2 else ''
|
||||||
else:
|
|
||||||
cookie_dict[attr_val[0]] = attr_val[1] if len(attr_val) == 2 else ''
|
|
||||||
|
|
||||||
return cookie_dict
|
return cookie_dict
|
||||||
|
|
||||||
@ -210,10 +207,17 @@ def cookies_to_tuple(cookies):
|
|||||||
cookies = tuple(cookie_to_dict(cookie) for cookie in cookies)
|
cookies = tuple(cookie_to_dict(cookie) for cookie in cookies)
|
||||||
|
|
||||||
elif isinstance(cookies, str):
|
elif isinstance(cookies, str):
|
||||||
cookies = tuple(cookie_to_dict(c.lstrip()) for c in cookies.rstrip(';,').split(',' if ',' in cookies else ';'))
|
c_dict = {}
|
||||||
|
for attr in cookies.strip().rstrip(';,').split(',' if ',' in cookies else ';'):
|
||||||
|
attr_val = attr.strip().split('=', 1)
|
||||||
|
c_dict[attr_val[0]] = attr_val[1] if len(attr_val) == 2 else ''
|
||||||
|
cookies = _dict_cookies_to_tuple(c_dict)
|
||||||
|
|
||||||
elif isinstance(cookies, dict):
|
elif isinstance(cookies, dict):
|
||||||
cookies = tuple({'name': cookie, 'value': cookies[cookie]} for cookie in cookies)
|
cookies = _dict_cookies_to_tuple(cookies)
|
||||||
|
|
||||||
|
elif isinstance(cookies, Cookie):
|
||||||
|
cookies = (cookie_to_dict(cookies),)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise TypeError('cookies参数必须为RequestsCookieJar、list、tuple、str或dict类型。')
|
raise TypeError('cookies参数必须为RequestsCookieJar、list、tuple、str或dict类型。')
|
||||||
@ -370,6 +374,7 @@ def tree(ele_or_page):
|
|||||||
:param ele_or_page: 页面或元素对象
|
:param ele_or_page: 页面或元素对象
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _tree(obj, last_one=True, body=''):
|
def _tree(obj, last_one=True, body=''):
|
||||||
list_ele = obj.children()
|
list_ele = obj.children()
|
||||||
length = len(list_ele)
|
length = len(list_ele)
|
||||||
@ -409,3 +414,15 @@ def format_headers(txt):
|
|||||||
name, value = header.split(': ', maxsplit=1)
|
name, value = header.split(': ', maxsplit=1)
|
||||||
headers[name] = value
|
headers[name] = value
|
||||||
return headers
|
return headers
|
||||||
|
|
||||||
|
|
||||||
|
def _dict_cookies_to_tuple(cookies: dict):
|
||||||
|
"""把dict形式的cookies转换为tuple形式
|
||||||
|
:param cookies: 单个或多个cookies,单个时包含'name'和'value'
|
||||||
|
:return: 多个dict格式cookies组成的列表
|
||||||
|
"""
|
||||||
|
if 'name' in cookies and 'value' in cookies: # 单个cookie
|
||||||
|
return (cookies,)
|
||||||
|
keys = ('domain', 'path', 'expires', 'max-age', 'HttpOnly', 'secure', 'expiry')
|
||||||
|
template = {k: v for k, v in cookies.items() if k in keys}
|
||||||
|
return tuple(dict(**{'name': k, 'value': v}, **template) for k, v in cookies.items() if k not in keys)
|
||||||
|
@ -37,7 +37,7 @@ def is_js_func(func: str) -> bool: ...
|
|||||||
def cookie_to_dict(cookie: Union[Cookie, str, dict]) -> dict: ...
|
def cookie_to_dict(cookie: Union[Cookie, str, dict]) -> dict: ...
|
||||||
|
|
||||||
|
|
||||||
def cookies_to_tuple(cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> tuple: ...
|
def cookies_to_tuple(cookies: Union[RequestsCookieJar, list, tuple, str, dict, Cookie]) -> tuple: ...
|
||||||
|
|
||||||
|
|
||||||
def set_session_cookies(session: Session, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ...
|
def set_session_cookies(session: Session, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ...
|
||||||
|
@ -22,8 +22,6 @@ class CookiesSetter(object):
|
|||||||
:param cookies: cookies信息
|
:param cookies: cookies信息
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
if (isinstance(cookies, dict) and 'name' in cookies and 'value' in cookies) or isinstance(cookies, Cookie):
|
|
||||||
cookies = [cookies]
|
|
||||||
set_browser_cookies(self._owner, cookies)
|
set_browser_cookies(self._owner, cookies)
|
||||||
|
|
||||||
def remove(self, name, url=None, domain=None, path=None):
|
def remove(self, name, url=None, domain=None, path=None):
|
||||||
@ -59,8 +57,6 @@ class SessionCookiesSetter(object):
|
|||||||
:param cookies: cookies信息
|
:param cookies: cookies信息
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
if (isinstance(cookies, dict) and 'name' in cookies and 'value' in cookies) or isinstance(cookies, Cookie):
|
|
||||||
cookies = [cookies]
|
|
||||||
set_session_cookies(self._owner.session, cookies)
|
set_session_cookies(self._owner.session, cookies)
|
||||||
|
|
||||||
def remove(self, name):
|
def remove(self, name):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user