如何捕获QWebEngineUrlRequestInterceptor截获的请求的响应?

时间:2019-07-07 16:07:14

标签: python pyqt pyqt5 qtwebengine

我有一个带有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

0 个答案:

没有答案
相关问题