Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

python - Wait for a delay before capture website image with PyQt

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:

image

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_()

QWebView Screenshot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...