Python Forum

Full Version: [PyQt5] [QWebEngineView] HTML file download
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

because previous Qt4 is old and not working on all sites, I'm looking a modern way to get Html AFTER Javascript.

This code works but the output file contains the Html before javascript rendering. :/
What's wrong ?

# https://stackoverflow.com/questions/37754138/how-to-render-html-with-pyqt5s-qwebengineview

def render(source_html):
    """Fully render HTML, JavaScript and all."""

    import sys
    from PyQt5.QtCore import QEventLoop
    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)
            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(source_html).html

import requests

i = 1
url = 'https://www.test.com/' + str(i) 
sample_html = requests.get(url).text

f = open(str(i) + ".html", "wt")
f.write(str(render(sample_html)))
f.close()
print('end')
Thanks for help. I'm going crazy after looking alone how to resolve this :(