在使用 PyQt 捕获网站图像之前等待延迟 [英] Wait for a delay before capture website image with PyQt

查看:20
本文介绍了在使用 PyQt 捕获网站图像之前等待延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt(作为 Python 初学者).我需要能够在无头系统上截取网站的屏幕截图.我之前在另一个项目中使用了 PhantomJS,但他们在 1.5 中放弃了对 Flash 的支持,我不想在我的新项目中依赖已弃用的 1.4 版本.

I'm working with PyQt (as a Python beginner). I need to be able to take screenshots of a website on a headless system. I was using PhantomJS earlier for another project, but they dropped Flash support in 1.5 and I don't want to rely on a deprecated 1.4 version for my new project.

所以我使用 PyQt 自己做我的事情.我可以使用给定的网址截取网站的屏幕截图,没问题.

So I'm using PyQt to do my stuff on my own. I'm able to take a screenshot of a website with a given url, no problem.

但我一直在 flash 占位符上显示blue dice" flash 插件图标(是的,javascript 和插件已激活)

But I keep having the "blue dice" flash plugin icon on flash placeholder (yes, javascript and plugins are activated)

self.settings.setAttribute(QtWebKit.QWebSettings.PluginsEnabled,True) 
self.settings.setAttribute(QtWebKit.QWebSettings.JavascriptEnabled,True) 

我正在 Youtube 视频页面上进行一些测试,以下是我遇到的问题的示例:

I'm making some test on a Youtube video page, here is an example of my issues:

第二部分,可能与第一部分有关:如何告诉 PyQt 在截取屏幕截图前等待几秒钟?正如您在示例中看到的那样,右侧的图像仍然未加载,因为它们是使用 javascript 和 data 属性加载的,并且在我的脚本中,我在 loadFinished 信号 (onLoad() javascript 中截取了屏幕截图等价)).

The second part, that may be related to the first one: How can I tell PyQt to wait few seconds before taking the screenshot ? As you can see on the example, images on the right are still unloaded, because they are loaded using javascript and data attribute and in my script, I take the screenshot on the loadFinished signal (onLoad() javascript equivalent)).

我的第一个猜测就是

time.sleep(2) 

在调用我的捕获方法之前,但它不起作用.我假设 Webkit 加载在此睡眠时间内也处于睡眠状态,从而阻止在页面上加载任何内容.

Before calling my capture method, but it's not working. I'm assuming that the Webkit loading is also asleep during this sleep time, preventing anything to load on the page.

我尝试创建一个自定义信号,但我仍然不知道如何在不睡觉的情况下触发它.

I tried to create a custom signal, but then I still don't know how to trigger it without sleeping.

我最后的猜测是我需要线程化我的应用程序.我说得对吗?

My last guess is that I need to thread my application. Am I right?

如果您有任何提示/脚本可以帮助我显示 Flash 内容和/或添加类似 setTimeout 的信号,我将不胜感激!

If you have any hint/script to help me displaying flash content and/or to add a setTimeout like signal, I would be really grateful!

预先感谢您的帮助.

只需快速编辑即可添加我的解决方案:

Just a quick edit to add my solution:

timeoutTimer = QTimer()
timeoutTimer.setInterval(3000) # wait for 3secs
timeoutTimer.setSingleShot(True)
timeoutTimer.timeout.connect(theMethodToCallOnTimeout)

关于 Flash 的事情:看起来 OSX 上的 Flash 播放器坏了(可能与 32/64 位问题有关).

About the flash thing: it looks like the flash player is broken on OSX (maybe related to a 32/64 bits issue).

推荐答案

如果你 time.sleep 你正在冻结整个应用程序,相反,你可以使用 QTimerQEventLoopQThread等,这里是PyQt4版本:

If you time.sleep you are freezing the whole application, instead, you can use a QTimer, QEventLoop, QThread, etc. Here is the PyQt4 version:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

class browser(QWebView):
    def __init__(self, parent=None):
        super(browser, self).__init__(parent)

        self.timerScreen = QTimer()
        self.timerScreen.setInterval(2000)
        self.timerScreen.setSingleShot(True)
        self.timerScreen.timeout.connect(self.takeScreenshot)

        self.loadFinished.connect(self.timerScreen.start)
        self.load(QUrl("http://www.google.com/ncr"))    

    def takeScreenshot(self):    
        image   = QImage(self.page().mainFrame().contentsSize(), QImage.Format_ARGB32)
        painter = QPainter(image)

        self.page().mainFrame().render(painter)

        painter.end()
        image.save(self.title() + ".png")

        sys.exit()

if __name__ == "__main__":
    import  sys        
    app  = QApplication(sys.argv)
    main = browser()
    app.exec_()

这篇关于在使用 PyQt 捕获网站图像之前等待延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆