PyQt QWebKit 框架bug?
我正在使用Python、PyQt4和QtWebKit来加载一个网页,目的是在一个简单的浏览器中查看数据。
不过,有一个小问题。我想获取加载页面中每个iframe的内容和源地址。我使用webView.page().mainFrame().childFrames()来获取这些框架。但问题是,childFrames()只会加载那些浏览器可见的框架。举个例子,当你的浏览器在页面顶部时,childFrames()不会加载位于页面底部的iframe。有没有什么方法或者设置可以调整,让我能获取到所有的广告?我附上了我的“浏览器”的源代码。请在页面加载完成后向下滚动。观察控制台,你会看到这些iframe是动态加载的。请帮帮我。
from PyQt4 import QtGui, QtCore, QtWebKit
import sys
import unicodedata
class Sp():
def Main(self):
self.webView = QtWebKit.QWebView()
self.webView.load(QtCore.QUrl("http://www.msnbc.msn.com/id/41197838/ns/us_news-environment/"))
self.webView.show()
QtCore.QObject.connect(self.webView,QtCore.SIGNAL("loadFinished(bool)"),self.Load)
def Load(self):
frame = self.webView.page().mainFrame()
children = frame.childFrames()
fT = []
for x in children:
print "=========================================="
print unicodedata.normalize('NFKD', unicode(x.url().toString())).encode('ascii','ignore')
print "=========================================="
fT.append([unicode(x.url().toString()),unicode(x.toHtml()),[]])
for x in range(len(fT)):
f = children[x]
tl = []
for fx in f.childFrames():
print "___________________________________________"
print unicodedata.normalize('NFKD', unicode(fx.url().toString())).encode('ascii','ignore')
print "___________________________________________"
tl.append([unicode(fx.url().toString()),unicode(fx.toHtml()),[]])
fT[x][2] = tl
app = QtGui.QApplication(sys.argv)
s = Sp()
s.Main()
app.exec_()
1 个回答
0
我不太明白你在做什么,但如果你只是想加载可见的部分,你可以把页面的视口大小设置成内容的大小,这样就能加载所有内容了:
def Load(self):
self.webView.page().setViewportSize(
self.webView.page().mainFrame().contentsSize())
不过,这样在图形界面上会有一些奇怪的效果,所以这个方法可能不太适合你想要做的事情。