在用PyQt捕捉网站图像之前等待一段时间 [英] Wait for a delay before capture website image with PyQt

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

问题描述

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



所以我使用PyQt自己做我的东西。
我可以截取一个给定网址的网站,没有问题。



但我一直有蓝色骰子的Flash插件图标在Flash占位符(是的,JavaScript和插件被激活)

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

我是在Youtube视频页面做一些测试,这里是我的问题的一个例子:



如何让PyQt等待几​​秒钟截图?
正如您在示例中看到的那样,右侧的图像仍然是未加载的,因为它们是使用javascript和data属性加载的,而且在我的脚本中,我使用 loadFinished 信号截图

我的第一个猜测就是简单的给

  time.sleep(2)

在调用我的捕获方法之前,它不工作。我假设在这个睡眠时间,Webkit的加载也是睡着了,阻止了任何东西加载在页面上。



我试图创建一个自定义信号,但后来我仍然不知道如何触发它没有睡觉。

我最后的猜测是,我需要线程我的应用程序。我是对的吗?

如果你有任何提示/脚本来帮助我显示flash内容和/或添加一个setTimeout类似的信号,我会很感激!

感谢您的帮助。快速编辑添加我的解决方案:

pre $ timeoutTimer = QTimer()
timeoutTimer.setInterval(3000)#等待3秒
timeoutTimer.setSingleShot(True)
timeoutTimer.timeout.connect(theMethodToCallOnTimeout)

关于闪光的事情:它看起来像是在OSX上的Flash播放器(可能与32/64位问题有关)。

如果你 time.sleep 你冻结整个应用程序,你可以使用 QTimer QEventLoop QThread 等等这里是PyQt4版本:

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

from PyQt4.QtCore import *
从PyQt4.QtGui导入*
从PyQt4.QtWebKit导入*

类浏览器(QWebView):
def __init __(self,parent = None):
super(browser,self).__ init __(parent)
$ b $ 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)
$ b self.page()。mainFrame()。render(画家)

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

sys.exit()

if __name__ ==__main__:
导入sys
app = QApplication(sys.argv)
main = browser()
app.exec_()


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.

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.

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) 

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

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)).

My first guess was simply to

time.sleep(2) 

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?

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!

Thanks in advance for you help.

EDIT: Just a quick edit to add my solution:

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

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

解决方案

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天全站免登陆