Python - 如何在QWebEnginePage中使用mainframe()方法[mainframe()错误]
Python - how to use mainframe() method in QWebEnginePage [error of mainframe()]
我在 PyQt5 代码中遇到错误。谁能帮我。
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView
class Browser(QWebView):
def __init__(self):
QWebView.__init__(self)
self.loadFinished.connect(self._result_available)
def _result_available(self, ok):
frame = self.page().mainFrame()
print( unicode(frame.toHtml()).encode('utf-8'))
if __name__ == '__main__':
app = QApplication(sys.argv)
view = Browser()
view.load(QUrl('http://www.google.com'))
app.exec_()
输出:[错误]
AttributeError Traceback (most recent call last)
<ipython-input-50-e1b5f3fc9054> in _result_available(self, ok)
13
14 def _result_available(self, ok):
---> 15 frame = self.page().mainFrame() ------------- [ERROR]
16 print( unicode(frame.toHtml()).encode('utf-8'))
17
AttributeError: 'QWebEnginePage' object has no attribute 'mainFrame'
看来您使用的 Qt Webkit 指南已从 Qt 5.6 弃用,目前使用的 Qt WebEngine 已经更改了许多 类 和方法,因为它基于 Chromium,在此 link
你可以找到如何将 Qt Webkit 移植到 Qt WebEngine 的指南。在你的情况下没有mainFrame()
,获取HTML的方式是异步的:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView
class Browser(QWebView):
def __init__(self):
QWebView.__init__(self)
self.loadFinished.connect(self._result_available)
def _result_available(self, ok):
if ok:
frame = self.page()
frame.toHtml(self.callback)
def callback(self, html):
print(unicode(html).encode('utf-8'))
if __name__ == '__main__':
app = QApplication(sys.argv)
view = Browser()
view.load(QUrl('http://www.google.com'))
sys.exit(app.exec_())
我在 PyQt5 代码中遇到错误。谁能帮我。
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView
class Browser(QWebView):
def __init__(self):
QWebView.__init__(self)
self.loadFinished.connect(self._result_available)
def _result_available(self, ok):
frame = self.page().mainFrame()
print( unicode(frame.toHtml()).encode('utf-8'))
if __name__ == '__main__':
app = QApplication(sys.argv)
view = Browser()
view.load(QUrl('http://www.google.com'))
app.exec_()
输出:[错误]
AttributeError Traceback (most recent call last)
<ipython-input-50-e1b5f3fc9054> in _result_available(self, ok)
13
14 def _result_available(self, ok):
---> 15 frame = self.page().mainFrame() ------------- [ERROR]
16 print( unicode(frame.toHtml()).encode('utf-8'))
17
AttributeError: 'QWebEnginePage' object has no attribute 'mainFrame'
看来您使用的 Qt Webkit 指南已从 Qt 5.6 弃用,目前使用的 Qt WebEngine 已经更改了许多 类 和方法,因为它基于 Chromium,在此 link
你可以找到如何将 Qt Webkit 移植到 Qt WebEngine 的指南。在你的情况下没有mainFrame()
,获取HTML的方式是异步的:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView
class Browser(QWebView):
def __init__(self):
QWebView.__init__(self)
self.loadFinished.connect(self._result_available)
def _result_available(self, ok):
if ok:
frame = self.page()
frame.toHtml(self.callback)
def callback(self, html):
print(unicode(html).encode('utf-8'))
if __name__ == '__main__':
app = QApplication(sys.argv)
view = Browser()
view.load(QUrl('http://www.google.com'))
sys.exit(app.exec_())