Python Forum
[PyQt5] [QWebEngineView] HTML file download
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt5] [QWebEngineView] HTML file download
#1
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 :(
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Saving file to html or pdf stopped working in PyQt6 ejKDE 4 271 Mar-12-2024, 07:45 PM
Last Post: ejKDE
  [PyQt] Control a combobox from another python file PyQt5 prath 0 2,242 May-05-2020, 03:22 PM
Last Post: prath
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,785 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [PyQt] enter from py exported by pyqt5 designer to another py file royer14 0 2,197 Jun-29-2018, 01:45 AM
Last Post: royer14
  PyQt5 - import rext from other file - despearte for help D_frucht 1 2,447 May-26-2018, 06:37 AM
Last Post: Barrowman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020