python - 如何捕获被QWebEngineUrlRequestInterceptor拦截的请求的响应?
问题描述
我有一个带有 QWebEngineUrlRequestInterceptor 的 PyQt5 QWebEngineProfile。这个拦截器让我可以在请求解决之前访问它。是否可以捕获每个被截获的请求的响应而无需手动重新提交请求?
class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
def __init__(self, on_network_call):
super().__init__()
self.on_network_call = on_network_call
def interceptRequest(self, info):
self.on_network_call(info)
class PyQtWebClient(QWebEnginePage):
def __init__(self, url):
self.app = QApplication(sys.argv)
self.interceptor = WebEngineUrlRequestInterceptor(self.on_network_call)
self.profile = QWebEngineProfile()
self.profile.setRequestInterceptor(self.interceptor)
super().__init__(self.profile, None)
self.loadFinished.connect(self._on_load_finished)
self.html = ""
self.network_requests = {}
self.load(QUrl(url))
self.app.exec_()
def on_network_call(self, info):
# Something ...
def _on_load_finished(self):
self.toHtml(self.callable)
def callable(self, html_str):
self.html = html_str
self.app.quit()
PyQt5 版本:5.11.2
解决方案
推荐阅读
- python - 我可以在 except 子句中抛出自定义异常而不会导致:“在处理...期间”
- html - 如何修复 Bootstrap 使其不会弄乱登录表单和页脚?
- javascript - 测试库错误:mockConstructor(...):没有从渲染返回
- python - 在词法作用域和 for 循环中苦苦挣扎
- bash - grep 获取流中一行中的前 X 个字符
- python - GraphQL Ariadne python获取'不能为不可为空的字段返回null'虽然值不为空
- java - 可变数据输入到记事本
- pine-script - 解构多行函数
- math - 如何根据深度信息找到平坦区域
- google-app-engine - 如何在 Google Cloud Scheduler 和 App Engine 之间建立私有通信?