P
P
Pavel Chirkov2019-08-31 09:18:50
Python
Pavel Chirkov, 2019-08-31 09:18:50

PyQt5 QWebEngineView open page - how to get source code?

Good afternoon
everyone PyQt5 QWebEngineView open page

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEngineView, QWebEnginePage
from PyQt5.QtGui import QIcon
import PyQt5
import sys


class Main(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Name')
        self.setWindowIcon(QIcon('icon.png'))

        web = QWebEngineView()

        web.load(QUrl("https://google.com"))
        html = web.page().toHtml()

        self.btn = QPushButton('Button', self)
        self.btn.resize(self.btn.sizeHint())
        lay = QVBoxLayout(self)
        lay.addWidget(self.btn)
        lay.addWidget(web)

app = QApplication(sys.argv)
main = Main()
main.show()
app.exec_()

But there is an error toHtml(self, Callable[[str], None]): not enough arguments
How to get the page's html code correctly?
app = QApplication(sys.argv)
main = Main()
main.show()
app.exec_()

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
B
bbkmzzzz, 2019-08-31
@bbkmzzzz

toHtml method is asynchronous, it needs a callback function as an argument, which will accept a string as input, which will be the page code

def processHTML(html):
    print(html)
web.page().toHtml(processHTML)

When the page code is ready, PyQt will call processHTML

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question