public
Last active

Takes snapshot of full webpages using Webkit and Qt4

  • Download Gist
web2png.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#!/usr/bin/env python
"""
Takes snapshot of full webpages using Webkit and Qt4
usage:
python web2png.py URL [FILE]
example:
python web2png.py http://python.org out.png
 
see:
http://riverbankcomputing.com/static/Docs/PyQt4/html/qwebview.html#load
http://riverbankcomputing.com/static/Docs/PyQt4/html/qwebpage.html#loadFinished
http://riverbankcomputing.com/static/Docs/PyQt4/html/qwebframe.html#render
https://github.com/AdamN/python-webkit2png
http://www.blogs.uni-osnabrueck.de/rotapken/2008/12/03/create-screenshots-of-a-web-page-using-python-and-qtwebkit/
http://thechangelog.com/post/512693979/webkit2png-command-line-web-screenshots
"""
 
import sys
import time
try:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
except ImportError:
print("sudo apt-get install python-qt4 libqt4-webkit")
sys.exit(1)
 
class Webshot(QObject):
def __init__(self, url="http://python.org", out=None):
QObject.__init__(self)
self.url = url
if not out:
out = "web2png.%i.png"%int(time.time())
self.out = out
self.view = None
self.page = None
 
def shot(self):
self.view = QWebView()
self.view.load(QUrl(self.url))
self.view.show()
self.page = self.view.page()
self.connect(self.page, SIGNAL("loadFinished(bool)"), self.render)
print("loading...")
 
def render(self):
print("... done")
frame = self.page.currentFrame()
size = frame.contentsSize()
size.setWidth(size.width()+20)
size.setHeight(size.height()+10)
self.view.resize(size)
self.page.setViewportSize(size)
img = QImage(size, QImage.Format_ARGB32)
paint = QPainter(img)
print("rendering...")
frame.render(paint, QWebFrame.ContentsLayer)
paint.end()
img.save(self.out)
print("... done")
print("result: %s"%self.out)
QApplication.quit()
 
def main(argv):
app = QApplication(argv)
if len(argv) < 2:
print(__doc__)
return 2
elif len(argv) < 3:
t = Webshot(argv[1])
else:
t = Webshot(argv[1], argv[2])
t.shot()
return app.exec_()
 
if __name__ == '__main__':
sys.exit(main(sys.argv))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.