How to “render” HTML with PyQt5's QWebEngineView

前端 未结 3 1913
野性不改
野性不改 2020-12-14 21:13

How can I \"render\" HTML with with PyQt5 v5.6 QWebEngineView?

I have previously performed the task with PyQt5 v5.4.1 QWebPage, but it was suggested to try the newe

3条回答
  •  暖寄归人
    2020-12-14 22:07

    As you pointed out, Qt5.4 relies on async calls. It's not necessary to use the Loop (as seen on your answer), since your only mistake was to call quit before the toHtml call finishes.

    def render(source_html):
        """Fully render HTML, JavaScript and all."""
    
        import sys
        from PyQt5.QtWidgets import QApplication
        from PyQt5.QtWebEngineWidgets import QWebEngineView
    
        class Render(QWebEngineView):
            def __init__(self, html):
                self.html = None
                self.app = QApplication(sys.argv)
                QWebEngineView.__init__(self)
                self.loadFinished.connect(self._loadFinished)
                self.setHtml(html)
                self.app.exec_()
    
            def _loadFinished(self, result):
                # This is an async call, you need to wait for this
                # to be called before closing the app
                self.page().toHtml(self.callable)
    
            def callable(self, data):
                self.html = data
                # Data has been stored, it's safe to quit the app
                self.app.quit()
    
        return Render(source_html).html
    
    import requests
    sample_html = requests.get(dummy_url).text
    print(render(sample_html))
    

提交回复
热议问题