How to capture the response of a request intercepted by QWebEngineUrlRequestInterceptor?

巧了我就是萌 提交于 2019-12-13 20:18:32

问题


I have a PyQt5 QWebEngineProfile with a QWebEngineUrlRequestInterceptor. This interceptor gives me access to the request before it it resolved. Is it possible to capture the response of every request intercepted without having to manually re-submit the request?

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 Version: 5.11.2

来源:https://stackoverflow.com/questions/56923879/how-to-capture-the-response-of-a-request-intercepted-by-qwebengineurlrequestinte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!