当使用PyQT实现无头webkit时,HTML页面有很大的不同

2024-09-27 00:13:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我的印象是,使用PyQT实现webkit的无头浏览器将自动获得每个URL的html代码,即使其中包含大量JS代码。但我只看到了一部分。我正在与从firefox窗口保存页面时得到的页面进行比较。在

我正在使用以下代码-

class JabbaWebkit(QWebPage):
    # 'html' is a class variable

    def __init__(self, url, wait, app, parent=None):
        super(JabbaWebkit, self).__init__(parent)
        JabbaWebkit.html = ''

        if wait:
            QTimer.singleShot(wait * SEC, app.quit)
        else:
            self.loadFinished.connect(app.quit)

        self.mainFrame().load(QUrl(url))

    def save(self):
        JabbaWebkit.html = self.mainFrame().toHtml()

    def userAgentForUrl(self, url):
        return USER_AGENT


    def get_page(url, wait=None):
        # here is the trick how to call it several times
        app = QApplication.instance() # checks if QApplication already exists

        if not app: # create QApplication if it doesnt exist
            app = QApplication(sys.argv)
        #
        form = JabbaWebkit(url, wait, app)
        app.aboutToQuit.connect(form.save)
        app.exec_()
        return JabbaWebkit.html

有人能看出代码有什么明显的错误吗?在

在通过几个url运行代码之后,我发现了一个非常清楚地显示了我遇到的问题-http://www.chilis.com/EN/Pages/menu.aspx

谢谢你的指点。在


Tags: 代码selfappurlifinitisdef
1条回答
网友
1楼 · 发布于 2024-09-27 00:13:51

页面有ajax代码,当它完成加载后,仍然需要一些时间来用ajax更新页面。但是你的代码会在它完成加载后退出。在

您应该添加一些类似这样的代码,以便在webkit中等待一些时间并处理事件:

for i in range(200): #wait 2 seconds
    app.processEvents()
    time.sleep(0.01)

相关问题 更多 >

    热门问题