def _render(url, wait):
try:
from PyQt5.QtWebEngineWidgets import QWebEngineView
from sip import delete
class Render():
page_load_timeout = 15
def __init__(self, url, wait):
self.wait = wait
self.html = None
self._createView()
while self.html is None:
self.app.processEvents(
self.app.quit()
def _createView(self):
self.view = QWebEngineView()
self.view.loadFinished.connect(self._loadFinished)
self.view.loadProgress.connect(self._loadProgress)
self.view.load(self.url)
def _checkProgress(self):
cur = time.clock()
if self._lastProg + self.page_load_timeout < cur:
self._lastProg = time.clock()
with open(os.getcwd() + "\\files\\wk_log.txt", "a") as f:
f.write("Recreating view\n")
delete(self.view)
self._createView()
def _loadProgress(self, prog):
with open(os.getcwd() + "\\files\\wk_log.txt", "a") as f:
f.write("{}\n".format(prog))
if prog != 100:
self._lastProg = time.clock()
QTimer.
singleShot((self.
page_load_timeout + 0.5) * 1000, self._checkProgress
)
def _loadFinished(self, result):
QTimer.
singleShot(self.
wait * 1000, self._timer_for_html
)
def _timer_for_html(self):
self.view.page().toHtml(self._callable)
def _callable(self, data):
self.html = data
except:
print("Exception occured")
#with devnull():
return Render(url, wait).html
def _render(url, wait):
try:
from PyQt5.QtCore import QEventLoop, QUrl, QTimer
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
from sip import delete
class Render():
page_load_timeout = 15
def __init__(self, url, wait):
self.url = QUrl(url)
self.wait = wait
self.html = None
self.app = QApplication(sys.argv)
self._createView()
while self.html is None:
self.app.processEvents(
QEventLoop.ExcludeUserInputEvents |
QEventLoop.ExcludeSocketNotifiers |
QEventLoop.WaitForMoreEvents)
self.app.quit()
def _createView(self):
self.view = QWebEngineView()
self.view.loadFinished.connect(self._loadFinished)
self.view.loadProgress.connect(self._loadProgress)
self.view.load(self.url)
def _checkProgress(self):
cur = time.clock()
if self._lastProg + self.page_load_timeout < cur:
self._lastProg = time.clock()
with open(os.getcwd() + "\\files\\wk_log.txt", "a") as f:
f.write("Recreating view\n")
delete(self.view)
self._createView()
def _loadProgress(self, prog):
with open(os.getcwd() + "\\files\\wk_log.txt", "a") as f:
f.write("{}\n".format(prog))
if prog != 100:
self._lastProg = time.clock()
QTimer.singleShot((self.page_load_timeout + 0.5) * 1000, self._checkProgress)
def _loadFinished(self, result):
QTimer.singleShot(self.wait * 1000, self._timer_for_html)
def _timer_for_html(self):
self.view.page().toHtml(self._callable)
def _callable(self, data):
self.html = data
except:
print("Exception occured")
#with devnull():
return Render(url, wait).html
To copy to clipboard, switch view to plain text mode
Never mind the layout of the code, it's just to get my problem across. The webview will load fine a couple of times, then suddenly break. Now the current code fixes this, however it doesn't close the previous view properly. I've thought about running del self.view However I don't think that would change anything as it would just delete the reference of the view and not the actual view itself.
Bookmarks