首页 > 解决方案 > 如何捕获被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

标签: pythonpyqtpyqt5qtwebengine

解决方案


推荐阅读