diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index c23fca9..9ac42a2 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] -__version__ = '4.0.4.11' +__version__ = '4.0.4.12' diff --git a/DrissionPage/_functions/web.py b/DrissionPage/_functions/web.py index 08d05fc..f84f170 100644 --- a/DrissionPage/_functions/web.py +++ b/DrissionPage/_functions/web.py @@ -181,17 +181,14 @@ def cookie_to_dict(cookie): cookie_dict = cookie elif isinstance(cookie, str): - cookie = cookie.rstrip(';,').split(',' if ',' in cookie else ';') cookie_dict = {} - - for key, attr in enumerate(cookie): - attr_val = attr.lstrip().split('=', 1) - - if key == 0: + for attr in cookie.strip().rstrip(';,').split(',' if ',' in cookie else ';'): + attr_val = attr.strip().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 '' + else: cookie_dict['name'] = attr_val[0] 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 @@ -210,10 +207,17 @@ def cookies_to_tuple(cookies): cookies = tuple(cookie_to_dict(cookie) for cookie in cookies) 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): - 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: raise TypeError('cookies参数必须为RequestsCookieJar、list、tuple、str或dict类型。') @@ -370,6 +374,7 @@ def tree(ele_or_page): :param ele_or_page: 页面或元素对象 :return: None """ + def _tree(obj, last_one=True, body=''): list_ele = obj.children() length = len(list_ele) @@ -409,3 +414,15 @@ def format_headers(txt): name, value = header.split(': ', maxsplit=1) headers[name] = value 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) diff --git a/DrissionPage/_functions/web.pyi b/DrissionPage/_functions/web.pyi index b3a9914..8ce0c3a 100644 --- a/DrissionPage/_functions/web.pyi +++ b/DrissionPage/_functions/web.pyi @@ -37,7 +37,7 @@ def is_js_func(func: str) -> bool: ... 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: ... diff --git a/DrissionPage/_units/cookies_setter.py b/DrissionPage/_units/cookies_setter.py index ccdf70c..63afbb0 100644 --- a/DrissionPage/_units/cookies_setter.py +++ b/DrissionPage/_units/cookies_setter.py @@ -22,8 +22,6 @@ class CookiesSetter(object): :param cookies: cookies信息 :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) def remove(self, name, url=None, domain=None, path=None): @@ -59,8 +57,6 @@ class SessionCookiesSetter(object): :param cookies: cookies信息 :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) def remove(self, name):