添加get_cookies()方法

This commit is contained in:
g1879 2020-11-29 11:56:41 +08:00
parent 68ecb00069
commit d10985030c
5 changed files with 57 additions and 5 deletions

View File

@ -53,13 +53,20 @@ class DriverPage(object):
@property @property
def cookies(self) -> list: def cookies(self) -> list:
"""返回当前网站cookies""" """返回当前网站cookies"""
return self.driver.get_cookies() return self.get_cookies(True)
@property @property
def title(self) -> str: def title(self) -> str:
"""返回网页title""" """返回网页title"""
return self.driver.title return self.driver.title
def get_cookies(self, as_dict: bool = False) -> Union[list, dict]:
"""返回当前网站cookies"""
if as_dict:
return {cookie['name']: cookie['value'] for cookie in self.driver.get_cookies()}
else:
return self.driver.get_cookies()
def _try_to_connect(self, def _try_to_connect(self,
to_url: str, to_url: str,
times: int = 0, times: int = 0,

View File

@ -139,6 +139,13 @@ class MixPage(Null, SessionPage, DriverPage):
elif self._mode == 'd': elif self._mode == 'd':
return super(SessionPage, self).title return super(SessionPage, self).title
def get_cookies(self, as_dict: bool = False) -> Union[dict, list]:
"""返回cookies"""
if self._mode == 's':
return super().get_cookies(as_dict)
elif self._mode == 'd':
return super(SessionPage, self).get_cookies(as_dict)
def change_mode(self, mode: str = None, go: bool = True) -> None: def change_mode(self, mode: str = None, go: bool = True) -> None:
"""切换模式,接收's''d'除此以外的字符串会切换为d模式 \n """切换模式,接收's''d'除此以外的字符串会切换为d模式 \n
切换时会把当前模式的cookies复制到目标模式 \n 切换时会把当前模式的cookies复制到目标模式 \n

View File

@ -16,6 +16,7 @@ from urllib.parse import urlparse, quote, unquote
from requests import Session, Response from requests import Session, Response
from .config import _cookie_to_dict
from .common import str_to_loc, translate_loc, get_available_file_name, format_html from .common import str_to_loc, translate_loc, get_available_file_name, format_html
from .session_element import SessionElement, execute_session_find from .session_element import SessionElement, execute_session_find
@ -54,18 +55,25 @@ class SessionPage(object):
@property @property
def cookies(self) -> dict: def cookies(self) -> dict:
"""返回session的cookies""" """返回session的cookies"""
return self.session.cookies.get_dict() return self.get_cookies(True)
@property @property
def title(self) -> str: def title(self) -> str:
"""返回网页title""" """返回网页title"""
return self.ele(('css selector', 'title')).text return self.ele('tag:title').text
@property @property
def html(self) -> str: def html(self) -> str:
"""返回页面html文本""" """返回页面html文本"""
return format_html(self.response.text) return format_html(self.response.text)
def get_cookies(self, as_dict: bool = False) -> Union[dict, list]:
"""返回session的cookies"""
if as_dict:
return self.session.cookies.get_dict()
else:
return [_cookie_to_dict(cookie) for cookie in self.session.cookies]
def ele(self, def ele(self,
loc_or_ele: Union[Tuple[str, str], str, SessionElement], loc_or_ele: Union[Tuple[str, str], str, SessionElement],
mode: str = None) -> Union[SessionElement, List[SessionElement or str], str, None]: mode: str = None) -> Union[SessionElement, List[SessionElement or str], str, None]:

View File

@ -547,6 +547,7 @@ page.current_tab_handle # Return to the current tab page handle
When calling a method that only belongs to d mode, it will automatically switch to d mode. See APIs for detailed usage. When calling a method that only belongs to d mode, it will automatically switch to d mode. See APIs for detailed usage.
```python ```python
page.get_cookies() # Get cookies, which can be returned by list or dict
page.change_mode() # Switch mode, it will automatically copy cookies page.change_mode() # Switch mode, it will automatically copy cookies
page.cookies_to_session() # Copy cookies from WebDriver object to Session object page.cookies_to_session() # Copy cookies from WebDriver object to Session object
page.cookies_to_driver() # Copy cookies from Session object to WebDriver object page.cookies_to_driver() # Copy cookies from Session object to WebDriver object
@ -887,7 +888,10 @@ options.set_paths(driver_path, chrome_path, debugger_address, download_path, use
### Instructions ### Instructions
```python ```python
do = DriverOptions(read_file=False) # Create chrome configuration object, do not read from ini file do = DriverOptions() # read the default ini file to create a DriverOptions object
do = DriverOptions('D:\\settings.ini') # read the specified ini file to create a DriverOptions object
do = DriverOptions(read_file=False) # Do not read the ini file, create an empty DriverOptions object
do.set_headless(False) # show the browser interface do.set_headless(False) # show the browser interface
do.set_no_imgs(True) # Do not load pictures do.set_no_imgs(True) # Do not load pictures
do.set_paths(driver_path='D:\\chromedriver.exe', chrome_path='D:\\chrome.exe') # set path do.set_paths(driver_path='D:\\chromedriver.exe', chrome_path='D:\\chrome.exe') # set path
@ -1341,6 +1345,16 @@ Returns: bool
### get_cookies()
Return cookies.
Parameter Description:
- as_dict: bool-Whether to return as dict, the default is to return complete cookies as list
### change_mode() ### change_mode()
Switch mode,'d' or's'. When switching, the cookies of the current mode will be copied to the target mode. Switch mode,'d' or's'. When switching, the cookies of the current mode will be copied to the target mode.

View File

@ -19,6 +19,8 @@ DrissionPage即 driver 和 session 的合体。
**联系邮箱:** g1879@qq.com **联系邮箱:** g1879@qq.com
**交流QQ群**897838127
# 理念及背景 # 理念及背景
*** ***
@ -549,6 +551,7 @@ page.current_tab_handle # 返回当前标签页 handle
调用只属于 d 模式的方法,会自动切换到 d 模式。详细用法见 APIs。 调用只属于 d 模式的方法,会自动切换到 d 模式。详细用法见 APIs。
```python ```python
page.get_cookies() # 获取 cookies可以 list 或 dict 方式返回
page.change_mode() # 切换模式,会自动复制 cookies page.change_mode() # 切换模式,会自动复制 cookies
page.cookies_to_session() # 从 WebDriver 对象复制 cookies 到 Session 对象 page.cookies_to_session() # 从 WebDriver 对象复制 cookies 到 Session 对象
page.cookies_to_driver() # 从 Session 对象复制 cookies 到 WebDriver 对象 page.cookies_to_driver() # 从 Session 对象复制 cookies 到 WebDriver 对象
@ -888,7 +891,10 @@ options.set_paths(driver_path, chrome_path, debugger_address, download_path, use
### 使用方法 ### 使用方法
```python ```python
do = DriverOptions(read_file=False) # 创建chrome配置对象不从 ini 文件读取 do = DriverOptions() # 读取默认 ini 文件创建 DriverOptions 对象
do = DriverOptions('D:\\settings.ini') # 读取指定 ini 文件创建 DriverOptions 对象
do = DriverOptions(read_file=False) # 不读取 ini 文件,创建空的 DriverOptions 对象
do.set_headless(False) # 显示浏览器界面 do.set_headless(False) # 显示浏览器界面
do.set_no_imgs(True) # 不加载图片 do.set_no_imgs(True) # 不加载图片
do.set_paths(driver_path='D:\\chromedriver.exe', chrome_path='D:\\chrome.exe') # 设置路径 do.set_paths(driver_path='D:\\chromedriver.exe', chrome_path='D:\\chrome.exe') # 设置路径
@ -1356,6 +1362,16 @@ MixPage 封装了页面操作的常用功能,可在 driver 和 session 模式
### get_cookies()
返回 cookies。
参数说明:
- as_dict: bool - 是否以 dict 方式返回,默认以 list 返回完整的 cookies
### change_mode() ### change_mode()
切换模式,'d' 或 's'。切换时会把当前模式的 cookies 复制到目标模式。 切换模式,'d' 或 's'。切换时会把当前模式的 cookies 复制到目标模式。