NameError:name' QWebPage'没有定义

时间:2017-01-20 01:47:37

标签: python python-3.x

我是Python新手,我想了解为什么会出现以下错误:

Traceback (most recent call last):
File "WebScraper.py", line 10, in <module>
    class Render(QWebPage):  
NameError: name 'QWebPage' is not defined

以下是代码:

import sys  
from PyQt5.QtGui import *  
from PyQt5.QtCore import *  
from PyQt5.QtWebEngineWidgets import *  
from lxml import html 

#Take this class for granted.Just use result of rendering.
class Render(QWebPage):  
  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()  

url = 'http://pycoders.com/archive/'  
r = Render(url)  
result = r.frame.toHtml()
#This step is important.Converting QString to Ascii for lxml to process
archive_links = html.fromstring(str(result.toAscii()))
print(archive_links)

我理解__init__充当构造函数,但为什么不将它设置为self?所以我需要将其更改为QWebPage.x = self

2 个答案:

答案 0 :(得分:1)

您未导入QWebPage

尝试将此导入添加到脚本顶部:

from PyQt5.QtWebKitWidgets import QWebPage

答案 1 :(得分:0)

您使用的是什么版本的PyQt5?请注意,启动PyQt5.9,QtWebKitWidgets(以及QtWebKit)不再可用(不建议使用),导致您收到错误。

让我并置两个渲染函数,一个使用旧的(PyQt4),另一个使用最新的PyQt5:

使用PyQt4,请注意 QWebPage 的使用情况,当然还有导入

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

class Render(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()

使用PyQt5,请注意使用 QWebEngineView 而不是 QWebPage ,当然还有导入

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

    class Render(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 is None:
                self.app.processEvents(
                    QEventLoop.ExcludeUserInputEvents |
                    QEventLoop.ExcludeSocketNotifiers |
                    QEventLoop.WaitForMoreEvents)
            self.app.quit()