我正在 PySide 中加载网页。我希望能够查看它是否已成功加载而没有错误。如果我得到一个错误,那么我想知道它。我不能简单地对加载完成信号使用“成功”参数,因为它似乎总是表明成功。我知道该怎么做的唯一方法是做这样的事情:
class WebPageResourceTracker(
PySide.QtCore.QObject
):
def __init__(self, url):
PySide.QtCore.QObject.__init__(self)
self.page = PySide.QtWebKit.QWebPage()
self.frame = self.page.currentFrame()
# The reason for the lambda is that otherwise I get this:
#ASSERT: "self" in file /var/tmp/portage/dev-python/pyside-1.1.0/work/pyside-qt4.7+1.1.0/libpyside/signalmanager.cpp, line 534
self.frame.loadFinished.connect(lambda ok: self.onLoadFinished(ok))
self.page.networkAccessManager().finished.connect(lambda reply: self.onResourceFinished(reply))
self.frame.setUrl(
PySide.QtCore.QUrl(
url,
)
)
def onLoadFinished(self, ok):
print 'onLoadFinished', ok
def onResourceFinished(self, reply):
print 'onResourceFinished', reply.url().toString(), reply.error()
import sys
from PySide import QtGui
app = QtGui.QApplication(sys.argv)
#WebPageResourceTracker("http://google.com")
WebPageResourceTracker(
"http://migration.tanagerproductions.com/broken.html"
)
sys.exit(app.exec_())
但是,我想有一种方法可以直接访问页面的资源列表,因为 webkit 检查器可以列出资源......
奖励:我还想知道页面中是否有任何 JavaScript 错误,我该怎么做?