How to “render” HTML with PyQt5's QWebEngineView

前端 未结 3 1910
野性不改
野性不改 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 21:54

    The answer by Six & Veehmot is great, but I found out that for my purpose it was not sufficient, as it did not expand the dropdown elements of the page that I wanted to scrape. A slight modification fixed this:

    def render(url):
        """Fully render HTML, JavaScript and all."""
    
        import sys
        from PyQt5.QtCore import QEventLoop,QUrl
        from PyQt5.QtWidgets import QApplication
        from PyQt5.QtWebEngineWidgets import QWebEngineView
    
        class Render(QWebEngineView):
            def __init__(self, url):
                self.html = None
                self.app = QApplication(sys.argv)
                QWebEngineView.__init__(self)
                self.loadFinished.connect(self._loadFinished)
                self.load(QUrl(url))
                while self.html is None:
                    self.app.processEvents(QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers | QEventLoop.WaitForMoreEvents)
                self.app.quit()
    
            def _callable(self, data):
                self.html = data
    
            def _loadFinished(self, result):
                self.page().toHtml(self._callable)
    
        return Render(url).html
    
    
    print(render(dummy_url))
    

提交回复
热议问题