Skip to content Skip to sidebar Skip to footer

Nameerror: Name 'qwebpage' Is Not Defined

I am new to Python and am trying to understand why I am getting the following error: Traceback (most recent call last): File 'WebScraper.py', line 10, in class R

Solution 1:

You're not importing QWebPage

Try adding this import to the top of your script:

fromPyQt5.QtWebKitWidgetsimport QWebPage

Solution 2:

What version of PyQt5 are you using? Please note that starting PyQt5.9, QtWebKitWidgets (and also QtWebKit) is not longer available (deprecated), resulting in the error you are getting.

Let me juxtapose two rendering functions, one using old (PyQt4) and one using latest PyQt5:

Using PyQt4, note the usage of QWebPage and of course the imports


from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

classRender(QWebPage):
    """Render HTML with PyQt4 WebEngine."""def__init__(self, url):  
        self.app = QApplication(sys.argv)  
        QWebPage.__init__(self)  
        self.loadFinished.connect(self._loadFinished)  
        self.mainFrame().load(QUrl(url))  
        self.app.exec_()  

    def_loadFinished(self, result):  
        self.frame = self.mainFrame()  
        self.app.quit()

Using PyQt5, note the usage of QWebEngineView instead of QWebPage and of course the imports

from PyQt5.QtCore import QEventLoop
 from PyQt5.QtWebEngineWidgets import QWebEngineView
 from PyQt5.QtWidgets import QApplication

    classRender(QWebEngineView):
        """Render HTML with PyQt5 WebEngine."""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 isNone:
                self.app.processEvents(
                    QEventLoop.ExcludeUserInputEvents |
                    QEventLoop.ExcludeSocketNotifiers |
                    QEventLoop.WaitForMoreEvents)
            self.app.quit()

Post a Comment for "Nameerror: Name 'qwebpage' Is Not Defined"