mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
继续修改抓包功能,未完成
This commit is contained in:
parent
9d24bf908b
commit
cefb94515e
@ -1116,7 +1116,7 @@ class NetworkListener(object):
|
|||||||
break
|
break
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
|
|
||||||
if not self._results:
|
if self._caught == 0:
|
||||||
r = False
|
r = False
|
||||||
else:
|
else:
|
||||||
# todo
|
# todo
|
||||||
@ -1127,10 +1127,23 @@ class NetworkListener(object):
|
|||||||
self._caught = 0
|
self._caught = 0
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
def _requestWillBeSent(self, **kwargs):
|
||||||
|
"""接收到请求时的回调函数"""
|
||||||
|
for target in self._targets:
|
||||||
|
if (self._is_regex and search(target, kwargs['request']['url'])) or (
|
||||||
|
not self._is_regex and target in kwargs['request']['url']):
|
||||||
|
self._requests[kwargs['requestId']] = DataPacket(kwargs['requestId'], self._page.tab_id, target, kwargs)
|
||||||
|
|
||||||
|
if kwargs['request'].get('hasPostData', None) and not kwargs['request'].get('postData', None):
|
||||||
|
self._requests[kwargs['requestId']]._rawPostData \
|
||||||
|
= self._page.run_cdp('Network.getRequestPostData', requestId=kwargs['requestId'])['postData']
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
def _response_received(self, **kwargs):
|
def _response_received(self, **kwargs):
|
||||||
"""接收到返回信息时处理方法"""
|
"""接收到返回信息时处理方法"""
|
||||||
if kwargs['requestId'] in self._requests:
|
if kwargs['requestId'] in self._requests:
|
||||||
self._requests[kwargs['requestId']]['response'] = kwargs['response']
|
self._requests[kwargs['requestId']]._raw_response = kwargs
|
||||||
|
|
||||||
def _loading_finished(self, **kwargs):
|
def _loading_finished(self, **kwargs):
|
||||||
"""请求完成时处理方法"""
|
"""请求完成时处理方法"""
|
||||||
@ -1144,31 +1157,29 @@ class NetworkListener(object):
|
|||||||
body = ''
|
body = ''
|
||||||
is_base64 = False
|
is_base64 = False
|
||||||
|
|
||||||
request = self._requests[request_id]
|
data_packet = self._requests[request_id]
|
||||||
target = request['target']
|
data_packet._rowBody = body
|
||||||
rd = ResponseData(request_id, request['response'], body, self._page.tab_id, target)
|
data_packet._base64_body = is_base64
|
||||||
rd.postData = request['post_data']
|
|
||||||
rd._base64_body = is_base64
|
if data_packet.target in self._results:
|
||||||
rd.requestHeaders = request['request_headers']
|
self._results[data_packet.target].append(data_packet)
|
||||||
rd.method = request['method']
|
else:
|
||||||
self._results[target] = rd
|
self._results[data_packet.target] = [data_packet]
|
||||||
|
|
||||||
self._caught += 1
|
self._caught += 1
|
||||||
|
|
||||||
def _requestWillBeSent(self, **kwargs):
|
def _loading_failed(self, **kwargs):
|
||||||
"""接收到请求时的回调函数"""
|
"""请求失败时的处理方法"""
|
||||||
for target in self._targets:
|
if kwargs['requestId'] in self._requests:
|
||||||
if (self._is_regex and search(target, kwargs['request']['url'])) or (
|
data_packet = self._requests[kwargs['requestId']]
|
||||||
not self._is_regex and target in kwargs['request']['url']):
|
data_packet._raw_fail_info = kwargs
|
||||||
# self._requests[kwargs['requestId']] = {'target': target,
|
|
||||||
# 'post_data': kwargs['request'].get('postData', None),
|
if data_packet.target in self._results:
|
||||||
# 'request_headers': kwargs['request']['headers'],
|
self._results[data_packet.target].append(data_packet)
|
||||||
# 'method': kwargs['request']['method']}
|
else:
|
||||||
self._requests[kwargs['requestId']] = DataPacket(kwargs['requestId'], self._page.tab_id, target, kwargs)
|
self._results[data_packet.target] = [data_packet]
|
||||||
if kwargs['request'].get('hasPostData', None) and not kwargs['request'].get('postData', None):
|
|
||||||
pd = self._page.run_cdp('Network.getRequestPostData', requestId=kwargs['requestId'])['postData']
|
self._caught += 1
|
||||||
self._requests[kwargs['requestId']].
|
|
||||||
break
|
|
||||||
|
|
||||||
|
|
||||||
class ChromiumPageScroll(ChromiumScroll):
|
class ChromiumPageScroll(ChromiumScroll):
|
||||||
|
@ -16,6 +16,7 @@ from .chromium_driver import ChromiumDriver
|
|||||||
from .chromium_element import ChromiumElement, ChromiumScroll
|
from .chromium_element import ChromiumElement, ChromiumScroll
|
||||||
from .chromium_frame import ChromiumFrame
|
from .chromium_frame import ChromiumFrame
|
||||||
from .commons.constants import NoneElement
|
from .commons.constants import NoneElement
|
||||||
|
from .commons.web import DataPacket
|
||||||
from .session_element import SessionElement
|
from .session_element import SessionElement
|
||||||
|
|
||||||
|
|
||||||
@ -248,7 +249,7 @@ class NetworkListener(object):
|
|||||||
self._caught: int = ...
|
self._caught: int = ...
|
||||||
self._targets: Union[str, dict] = ...
|
self._targets: Union[str, dict] = ...
|
||||||
self._single: bool = ...
|
self._single: bool = ...
|
||||||
self._results: Union[ResponseData, Dict[str, ResponseData], False] = ...
|
self._results: Union[ResponseData, Dict[str, DataPacket], False] = ...
|
||||||
self._is_regex: bool = ...
|
self._is_regex: bool = ...
|
||||||
self._requests: dict = ...
|
self._requests: dict = ...
|
||||||
|
|
||||||
|
@ -18,7 +18,9 @@ from tldextract import extract
|
|||||||
class DataPacket(object):
|
class DataPacket(object):
|
||||||
"""返回的数据包管理类"""
|
"""返回的数据包管理类"""
|
||||||
__slots__ = ('requestId', 'request', 'response', 'rawBody', 'tab', 'target', '_requestHeaders', '_body',
|
__slots__ = ('requestId', 'request', 'response', 'rawBody', 'tab', 'target', '_requestHeaders', '_body',
|
||||||
'_base64_body', '_rawPostData', '_postData',
|
'_postData',
|
||||||
|
# cdp 原始数据
|
||||||
|
'_raw_request', '_raw_response', '_raw_fail_info', '_rawPostData', '_rawBody', '_base64_body',
|
||||||
|
|
||||||
'url', 'urlFragment', 'method', 'postDataEntries', 'mixedContentType', 'initialPriority',
|
'url', 'urlFragment', 'method', 'postDataEntries', 'mixedContentType', 'initialPriority',
|
||||||
'referrerPolicy', 'isLinkPreload', 'trustTokenParams', 'isSameSite',
|
'referrerPolicy', 'isLinkPreload', 'trustTokenParams', 'isSameSite',
|
||||||
@ -35,24 +37,40 @@ class DataPacket(object):
|
|||||||
:param request_id: request id
|
:param request_id: request id
|
||||||
:param tab: 产生这个数据包的tab的id
|
:param tab: 产生这个数据包的tab的id
|
||||||
:param target: 监听目标
|
:param target: 监听目标
|
||||||
:param raw_request: 原始request数据
|
:param raw_request: 原始request数据,从cdp获得
|
||||||
"""
|
"""
|
||||||
self.requestId = request_id
|
self.requestId = request_id
|
||||||
self._raw_request = raw_request
|
|
||||||
self.tab = tab
|
self.tab = tab
|
||||||
self.target = target
|
self.target = target
|
||||||
|
|
||||||
|
self._raw_request = raw_request
|
||||||
|
self._raw_response = None
|
||||||
|
self._raw_fail_info = None
|
||||||
|
self._rawPostData = None
|
||||||
|
self._rawBody = None
|
||||||
|
self._base64_body = False
|
||||||
|
|
||||||
self._requestHeaders = None
|
self._requestHeaders = None
|
||||||
self._postData = None
|
self._postData = None
|
||||||
self._body = None
|
self._body = None
|
||||||
self._base64_body = False
|
|
||||||
self._rawPostData = None
|
|
||||||
|
|
||||||
def __getattr__(self, item):
|
|
||||||
return self.response.get(item, None)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<ResponseData target={self.target} request_id={self.requestId}>'
|
return f'<DataPacket target={self.target} request_id={self.requestId}>'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def reuqest(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def response(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fail_info(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class RequestData(object):
|
||||||
@property
|
@property
|
||||||
def responseHeaders(self):
|
def responseHeaders(self):
|
||||||
"""以大小写不敏感字典返回headers数据"""
|
"""以大小写不敏感字典返回headers数据"""
|
||||||
@ -77,10 +95,6 @@ class DataPacket(object):
|
|||||||
self._postData = self._rawPostData
|
self._postData = self._rawPostData
|
||||||
return self._postData
|
return self._postData
|
||||||
|
|
||||||
def set_postData(self, val):
|
|
||||||
"""设置postData,当hasPostData为True但数据太长时使用"""
|
|
||||||
self._rawPostData = val
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def body(self):
|
def body(self):
|
||||||
"""返回body内容,如果是json格式,自动进行转换,如果时图片格式,进行base64转换,其它格式直接返回文本"""
|
"""返回body内容,如果是json格式,自动进行转换,如果时图片格式,进行base64转换,其它格式直接返回文本"""
|
||||||
@ -97,6 +111,14 @@ class DataPacket(object):
|
|||||||
return self._body
|
return self._body
|
||||||
|
|
||||||
|
|
||||||
|
class ResponseData(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class FailData(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_ele_txt(e):
|
def get_ele_txt(e):
|
||||||
"""获取元素内所有文本
|
"""获取元素内所有文本
|
||||||
:param e: 元素对象
|
:param e: 元素对象
|
||||||
|
Loading…
x
Reference in New Issue
Block a user