gpt4 book ai didi

python - 名称错误 : name 'QWebPage' is not defined

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:56 29 4
gpt4 key购买 nike

我是 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

最佳答案

您没有导入QWebPage

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

from PyQt5.QtWebKitWidgets import QWebPage

关于python - 名称错误 : name 'QWebPage' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41754786/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com