0

我正在使用 Python、PyQt4 和 QtWebKit 将网页加载到基本浏览器中以检查数据。

但是,有一个小问题。我正在尝试获取加载页面上每个 iframe 的内容和 src。我正在使用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_()
4

1 回答 1

0

不知道你为什么要做你正在做的事情,但如果它只加载可见的内容,你可以将页面视口大小设置为内容大小,这应该加载所有内容:

def Load(self):
    self.webView.page().setViewportSize(
        self.webView.page().mainFrame().contentsSize())

但是,这在 GUI 中会产生奇怪的效果,因此该解决方案对于您正在尝试做的事情可能是不可接受的。

于 2011-04-27T23:55:03.663 回答