diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index d9dcaf5..e5c4d5f 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -972,8 +972,18 @@ class ChromiumBaseSetter(object): js = f'localStorage.removeItem("{item}");' if item is False else f'localStorage.setItem("{item}","{value}");' return self._page.run_js_loaded(js, as_expr=True) + def cookie(self, cookie): + """设置单个cookie + :param cookie: cookie信息 + :return: None + """ + if isinstance(cookie, str): + self.cookies(cookie) + else: + self.cookies([cookie]) + def cookies(self, cookies): - """设置cookies值 + """设置多个cookie,注意不要传入单个 :param cookies: cookies信息 :return: None """ diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index 7102e84..eace25d 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -274,6 +274,8 @@ class ChromiumBaseSetter(object): def local_storage(self, item: str, value: Union[str, bool]) -> None: ... + def cookie(self, cookies: Union[RequestsCookieJar, str, dict]) -> None: ... + def cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... def headers(self, headers: dict) -> None: ... diff --git a/DrissionPage/chromium_tab.py b/DrissionPage/chromium_tab.py index a5ef752..814af68 100644 --- a/DrissionPage/chromium_tab.py +++ b/DrissionPage/chromium_tab.py @@ -360,7 +360,7 @@ class WebPageTabSetter(ChromiumBaseSetter): self._chromium_setter = ChromiumBaseSetter(self._page) def cookies(self, cookies): - """添加cookies信息到浏览器或session对象 + """添加多个cookies信息到浏览器或session对象,注意不要传入单个 :param cookies: 可以接收`CookieJar`、`list`、`tuple`、`str`、`dict`格式的`cookies` :return: None """ diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 9d296bb..2b50ef3 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -337,8 +337,18 @@ class SessionPageSetter(object): """ self._page.timeout = second + def cookie(self, cookie): + """为Session对象设置单个cookie + :param cookie: cookie信息 + :return: None + """ + if isinstance(cookie, str): + self.cookies(cookie) + else: + self.cookies([cookie]) + def cookies(self, cookies): - """为Session对象设置cookies + """为Session对象设置多个cookie,注意不要传入单个 :param cookies: cookies信息 :return: None """ diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index 41bacd7..5f9a2ce 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -3,6 +3,7 @@ @Author : g1879 @Contact : g1879@qq.com """ +from http.cookiejar import Cookie from typing import Any, Union, Tuple, List from DownloadKit import DownloadKit @@ -169,6 +170,8 @@ class SessionPageSetter(object): def timeout(self, second: float) -> None: ... + def cookie(self, cookie: Union[Cookie, str, dict]) -> None: ... + def cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... def headers(self, headers: dict) -> None: ...