mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
4.0.0b18(+)
SessionPage增加设置编码功能; listen.wait()增加raise_err参数; 优化set.local_storage()、set.session_storage()和clear_cache(); 各类增加__repr__; 指定新版DownloadKit
This commit is contained in:
parent
1a5ec884f1
commit
ae417542b7
@ -593,6 +593,9 @@ class ChromiumOptions(object):
|
|||||||
on_off = None if on_off else False
|
on_off = None if on_off else False
|
||||||
return self.set_argument('--mute-audio', on_off)
|
return self.set_argument('--mute-audio', on_off)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<ChromiumOptions at {id(self)}>'
|
||||||
|
|
||||||
|
|
||||||
class PortFinder(object):
|
class PortFinder(object):
|
||||||
used_port = {}
|
used_port = {}
|
||||||
|
@ -457,6 +457,9 @@ class SessionOptions(object):
|
|||||||
self._download_path = str(download_path)
|
self._download_path = str(download_path)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<SessionOptions at {id(self)}>'
|
||||||
|
|
||||||
|
|
||||||
def session_options_to_dict(options):
|
def session_options_to_dict(options):
|
||||||
"""把session配置对象转换为字典
|
"""把session配置对象转换为字典
|
||||||
|
@ -820,12 +820,18 @@ class ChromiumBase(BasePage):
|
|||||||
:param cookies: 是否清除cookies
|
:param cookies: 是否清除cookies
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
if session_storage:
|
if session_storage or local_storage:
|
||||||
self.run_js('sessionStorage.clear();', as_expr=True)
|
self.run_cdp_loaded('DOMStorage.enable')
|
||||||
if local_storage:
|
i = self.run_cdp('Storage.getStorageKeyForFrame', frameId=self._frame_id)['storageKey']
|
||||||
self.run_js('localStorage.clear();', as_expr=True)
|
if session_storage:
|
||||||
|
self.run_cdp('DOMStorage.clear', storageId={'storageKey': i, 'isLocalStorage': False})
|
||||||
|
if local_storage:
|
||||||
|
self.run_cdp('DOMStorage.clear', storageId={'storageKey': i, 'isLocalStorage': True})
|
||||||
|
self.run_cdp_loaded('DOMStorage.disable')
|
||||||
|
|
||||||
if cache:
|
if cache:
|
||||||
self.run_cdp_loaded('Network.clearBrowserCache')
|
self.run_cdp_loaded('Network.clearBrowserCache')
|
||||||
|
|
||||||
if cookies:
|
if cookies:
|
||||||
self.run_cdp_loaded('Network.clearBrowserCookies')
|
self.run_cdp_loaded('Network.clearBrowserCookies')
|
||||||
|
|
||||||
|
@ -248,6 +248,9 @@ class ChromiumPage(ChromiumBase):
|
|||||||
"""
|
"""
|
||||||
self.browser.quit(timeout, force)
|
self.browser.quit(timeout, force)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<ChromiumPage browser_id={self.browser.id} tab_id={self.tab_id}>'
|
||||||
|
|
||||||
|
|
||||||
def get_rename(original, rename):
|
def get_rename(original, rename):
|
||||||
if '.' in rename:
|
if '.' in rename:
|
||||||
|
@ -58,6 +58,9 @@ class ChromiumTab(ChromiumBase):
|
|||||||
self._wait = TabWaiter(self)
|
self._wait = TabWaiter(self)
|
||||||
return self._wait
|
return self._wait
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<ChromiumTab browser_id={self.browser.id} tab_id={self.tab_id}>'
|
||||||
|
|
||||||
|
|
||||||
class WebPageTab(SessionPage, ChromiumTab, BasePage):
|
class WebPageTab(SessionPage, ChromiumTab, BasePage):
|
||||||
def __init__(self, page, tab_id):
|
def __init__(self, page, tab_id):
|
||||||
@ -353,3 +356,6 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage):
|
|||||||
elif self._mode == 'd':
|
elif self._mode == 'd':
|
||||||
return super(SessionPage, self)._find_elements(loc_or_ele, timeout=timeout, single=single,
|
return super(SessionPage, self)._find_elements(loc_or_ele, timeout=timeout, single=single,
|
||||||
relative=relative)
|
relative=relative)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<WebPageTab browser_id={self.browser.id} tab_id={self.tab_id}>'
|
||||||
|
@ -32,6 +32,7 @@ class SessionPage(BasePage):
|
|||||||
self._response = None
|
self._response = None
|
||||||
self._session = None
|
self._session = None
|
||||||
self._set = None
|
self._set = None
|
||||||
|
self._encoding = None
|
||||||
self._s_set_start_options(session_or_options)
|
self._s_set_start_options(session_or_options)
|
||||||
self._s_set_runtime_settings()
|
self._s_set_runtime_settings()
|
||||||
self._create_session()
|
self._create_session()
|
||||||
@ -122,6 +123,11 @@ class SessionPage(BasePage):
|
|||||||
"""返回访问url得到的response对象"""
|
"""返回访问url得到的response对象"""
|
||||||
return self._response
|
return self._response
|
||||||
|
|
||||||
|
@property
|
||||||
|
def encoding(self):
|
||||||
|
"""返回设置的编码"""
|
||||||
|
return self._encoding
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def set(self):
|
def set(self):
|
||||||
"""返回用于等待的对象"""
|
"""返回用于等待的对象"""
|
||||||
@ -151,7 +157,6 @@ class SessionPage(BasePage):
|
|||||||
r.status_code = 200
|
r.status_code = 200
|
||||||
self._response = r
|
self._response = r
|
||||||
return
|
return
|
||||||
retry, interval = self._before_connect(url, retry, interval)
|
|
||||||
return self._s_connect(url, 'get', None, show_errmsg, retry, interval, **kwargs)
|
return self._s_connect(url, 'get', None, show_errmsg, retry, interval, **kwargs)
|
||||||
|
|
||||||
def ele(self, loc_or_ele, timeout=None):
|
def ele(self, loc_or_ele, timeout=None):
|
||||||
@ -233,7 +238,6 @@ class SessionPage(BasePage):
|
|||||||
:param kwargs: 连接参数
|
:param kwargs: 连接参数
|
||||||
:return: url是否可用
|
:return: url是否可用
|
||||||
"""
|
"""
|
||||||
retry, interval = self._before_connect(url, retry, interval)
|
|
||||||
return self._s_connect(url, 'post', data, show_errmsg, retry, interval, **kwargs)
|
return self._s_connect(url, 'post', data, show_errmsg, retry, interval, **kwargs)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
@ -289,7 +293,7 @@ class SessionPage(BasePage):
|
|||||||
:param data: post方式要提交的数据
|
:param data: post方式要提交的数据
|
||||||
:param show_errmsg: 是否显示和抛出异常
|
:param show_errmsg: 是否显示和抛出异常
|
||||||
:param kwargs: 其它参数
|
:param kwargs: 其它参数
|
||||||
:return: tuple,第一位为Response或None,第二位为出错信息或'Success'
|
:return: tuple,第一位为Response或None,第二位为出错信息或 'Success'
|
||||||
"""
|
"""
|
||||||
kwargs = CaseInsensitiveDict(kwargs)
|
kwargs = CaseInsensitiveDict(kwargs)
|
||||||
if 'headers' not in kwargs:
|
if 'headers' not in kwargs:
|
||||||
@ -322,6 +326,9 @@ class SessionPage(BasePage):
|
|||||||
r = self.session.post(url, data=data, **kwargs)
|
r = self.session.post(url, data=data, **kwargs)
|
||||||
|
|
||||||
if r and r.content:
|
if r and r.content:
|
||||||
|
if self._encoding:
|
||||||
|
r.encoding = self._encoding
|
||||||
|
return r, 'Success'
|
||||||
return set_charset(r), 'Success'
|
return set_charset(r), 'Success'
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -348,6 +355,9 @@ class SessionPage(BasePage):
|
|||||||
raise ConnectionError(f'状态码:{r.status_code}')
|
raise ConnectionError(f'状态码:{r.status_code}')
|
||||||
return r, f'状态码:{r.status_code}'
|
return r, f'状态码:{r.status_code}'
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<SessionPage url={self.url}>'
|
||||||
|
|
||||||
|
|
||||||
def check_headers(kwargs, headers, arg):
|
def check_headers(kwargs, headers, arg):
|
||||||
"""检查kwargs或headers中是否有arg所示属性"""
|
"""检查kwargs或headers中是否有arg所示属性"""
|
||||||
|
@ -30,6 +30,7 @@ class SessionPage(BasePage):
|
|||||||
self.retry_times: int = ...
|
self.retry_times: int = ...
|
||||||
self.retry_interval: float = ...
|
self.retry_interval: float = ...
|
||||||
self._set: SessionPageSetter = ...
|
self._set: SessionPageSetter = ...
|
||||||
|
self._encoding: str = ...
|
||||||
|
|
||||||
def _s_set_start_options(self, session_or_options: Union[Session, SessionOptions]) -> None: ...
|
def _s_set_start_options(self, session_or_options: Union[Session, SessionOptions]) -> None: ...
|
||||||
|
|
||||||
@ -114,6 +115,9 @@ class SessionPage(BasePage):
|
|||||||
@property
|
@property
|
||||||
def response(self) -> Response: ...
|
def response(self) -> Response: ...
|
||||||
|
|
||||||
|
@property
|
||||||
|
def encoding(self) -> str: ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def set(self) -> SessionPageSetter: ...
|
def set(self) -> SessionPageSetter: ...
|
||||||
|
|
||||||
|
@ -387,3 +387,6 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
super(SessionPage, self).quit(timeout, force)
|
super(SessionPage, self).quit(timeout, force)
|
||||||
self._driver = None
|
self._driver = None
|
||||||
self._has_driver = None
|
self._has_driver = None
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<WebPage browser_id={self.browser.id} tab_id={self.tab_id}>'
|
||||||
|
@ -12,6 +12,8 @@ from time import perf_counter, sleep
|
|||||||
from requests.structures import CaseInsensitiveDict
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
from .._base.chromium_driver import ChromiumDriver
|
from .._base.chromium_driver import ChromiumDriver
|
||||||
|
from .._functions.settings import Settings
|
||||||
|
from ..errors import WaitTimeoutError
|
||||||
|
|
||||||
|
|
||||||
class Listener(object):
|
class Listener(object):
|
||||||
@ -87,11 +89,12 @@ class Listener(object):
|
|||||||
|
|
||||||
self._set_callback()
|
self._set_callback()
|
||||||
|
|
||||||
def wait(self, count=1, timeout=None, fit_count=True):
|
def wait(self, count=1, timeout=None, fit_count=True, raise_err=None):
|
||||||
"""等待符合要求的数据包到达指定数量
|
"""等待符合要求的数据包到达指定数量
|
||||||
:param count: 需要捕捉的数据包数量
|
:param count: 需要捕捉的数据包数量
|
||||||
:param timeout: 超时时间,为None无限等待
|
:param timeout: 超时时间,为None无限等待
|
||||||
:param fit_count: 是否必须满足总数要求,发生超时,为True返回False,为False返回已捕捉到的数据包
|
:param fit_count: 是否必须满足总数要求,发生超时,为True返回False,为False返回已捕捉到的数据包
|
||||||
|
:param raise_err: 超时时是否抛出错误,为None时根据Settings设置
|
||||||
:return: count为1时返回数据包对象,大于1时返回列表,超时且fit_count为True时返回False
|
:return: count为1时返回数据包对象,大于1时返回列表,超时且fit_count为True时返回False
|
||||||
"""
|
"""
|
||||||
if not self.listening:
|
if not self.listening:
|
||||||
@ -113,7 +116,10 @@ class Listener(object):
|
|||||||
|
|
||||||
if fail:
|
if fail:
|
||||||
if fit_count or not self._caught.qsize():
|
if fit_count or not self._caught.qsize():
|
||||||
return False
|
if raise_err is True or Settings.raise_when_wait_failed is True:
|
||||||
|
raise WaitTimeoutError('等待数据包失败。')
|
||||||
|
else:
|
||||||
|
return False
|
||||||
else:
|
else:
|
||||||
return [self._caught.get_nowait() for _ in range(self._caught.qsize())]
|
return [self._caught.get_nowait() for _ in range(self._caught.qsize())]
|
||||||
|
|
||||||
|
@ -40,8 +40,8 @@ class Listener(object):
|
|||||||
|
|
||||||
def go_on(self) -> None: ...
|
def go_on(self) -> None: ...
|
||||||
|
|
||||||
def wait(self, count: int = 1, timeout: float = None,
|
def wait(self, count: int = 1, timeout: float = None, fit_count: bool = True,
|
||||||
fit_count: bool = True) -> Union[List[DataPacket], DataPacket, None]: ...
|
raise_err: bool = None) -> Union[List[DataPacket], DataPacket, None]: ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def results(self) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ...
|
def results(self) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ...
|
||||||
|
@ -82,8 +82,15 @@ class ChromiumBaseSetter(BasePageSetter):
|
|||||||
:param value: 项的值,设置为False时,删除该项
|
:param value: 项的值,设置为False时,删除该项
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
js = f'sessionStorage.removeItem("{item}");' if item is False else f'sessionStorage.setItem("{item}","{value}");'
|
self._page.run_cdp_loaded('DOMStorage.enable')
|
||||||
return self._page.run_js_loaded(js, as_expr=True)
|
i = self._page.run_cdp('Storage.getStorageKeyForFrame', frameId=self._page._frame_id)['storageKey']
|
||||||
|
if value is False:
|
||||||
|
self._page.run_cdp('DOMStorage.removeDOMStorageItem',
|
||||||
|
storageId={'storageKey': i, 'isLocalStorage': False}, key=item)
|
||||||
|
else:
|
||||||
|
self._page.run_cdp('DOMStorage.setDOMStorageItem', storageId={'storageKey': i, 'isLocalStorage': False},
|
||||||
|
key=item, value=value)
|
||||||
|
self._page.run_cdp_loaded('DOMStorage.disable')
|
||||||
|
|
||||||
def local_storage(self, item, value):
|
def local_storage(self, item, value):
|
||||||
"""设置或删除某项localStorage信息
|
"""设置或删除某项localStorage信息
|
||||||
@ -91,8 +98,15 @@ class ChromiumBaseSetter(BasePageSetter):
|
|||||||
:param value: 项的值,设置为False时,删除该项
|
:param value: 项的值,设置为False时,删除该项
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
js = f'localStorage.removeItem("{item}");' if item is False else f'localStorage.setItem("{item}","{value}");'
|
self._page.run_cdp_loaded('DOMStorage.enable')
|
||||||
return self._page.run_js_loaded(js, as_expr=True)
|
i = self._page.run_cdp('Storage.getStorageKeyForFrame', frameId=self._page._frame_id)['storageKey']
|
||||||
|
if value is False:
|
||||||
|
self._page.run_cdp('DOMStorage.removeDOMStorageItem',
|
||||||
|
storageId={'storageKey': i, 'isLocalStorage': True}, key=item)
|
||||||
|
else:
|
||||||
|
self._page.run_cdp('DOMStorage.setDOMStorageItem', storageId={'storageKey': i, 'isLocalStorage': True},
|
||||||
|
key=item, value=value)
|
||||||
|
self._page.run_cdp_loaded('DOMStorage.disable')
|
||||||
|
|
||||||
def cookie(self, cookie):
|
def cookie(self, cookie):
|
||||||
"""设置单个cookie
|
"""设置单个cookie
|
||||||
@ -237,6 +251,17 @@ class SessionPageSetter(BasePageSetter):
|
|||||||
"""
|
"""
|
||||||
self._page.timeout = second
|
self._page.timeout = second
|
||||||
|
|
||||||
|
def encoding(self, encoding, set_all=True):
|
||||||
|
"""设置编码
|
||||||
|
:param encoding: 编码名称
|
||||||
|
:param set_all: 是否设置对象参数,为False则只设置当前response
|
||||||
|
:return: None
|
||||||
|
"""
|
||||||
|
if set_all:
|
||||||
|
self._page._encoding = encoding
|
||||||
|
if self._page.response:
|
||||||
|
self._page.response.encoding = encoding
|
||||||
|
|
||||||
def cookie(self, cookie):
|
def cookie(self, cookie):
|
||||||
"""为Session对象设置单个cookie
|
"""为Session对象设置单个cookie
|
||||||
:param cookie: cookie信息
|
:param cookie: cookie信息
|
||||||
|
@ -100,6 +100,8 @@ class SessionPageSetter(BasePageSetter):
|
|||||||
|
|
||||||
def timeout(self, second: float) -> None: ...
|
def timeout(self, second: float) -> None: ...
|
||||||
|
|
||||||
|
def encoding(self, encoding: str, set_all: bool = True) -> None: ...
|
||||||
|
|
||||||
def cookie(self, cookie: Union[Cookie, str, dict]) -> None: ...
|
def cookie(self, cookie: Union[Cookie, str, dict]) -> None: ...
|
||||||
|
|
||||||
def cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ...
|
def cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ...
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
requests
|
requests
|
||||||
lxml
|
lxml
|
||||||
cssselect
|
cssselect
|
||||||
DownloadKit>=1.0.0
|
DownloadKit>=2.0.0b0
|
||||||
FlowViewer>=0.3.0
|
FlowViewer>=0.3.0
|
||||||
websocket-client
|
websocket-client
|
||||||
click
|
click
|
||||||
|
Loading…
x
Reference in New Issue
Block a user