May-08-2018, 08:41 PM
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 ?
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 :(
