Tuesday, September 22, 2009

Screenshot of Web Page in Python - 15 lines of code

I cant say that this solution is pretty elegant, but it's short and working. But... platform dependent, because of IE. Anyway here it is :

from win32com.client import Dispatch
from time import sleep
from sys import exit
import ImageGrab

IE=Dispatch("InternetExplorer.Application")
IE.Visible=1
IE.fullScreen = 1
IE.Navigate("http://ostas.blogspot.com")
while (IE.ReadyState != 4):
sleep(1)
img = ImageGrab.grab((0, 0, 1440, 900))
img.save('screenshot.jpg','JPEG')
IE.Quit();
exit(0);


Code is pretty simple- it launches IE in full screen mode, waiting while page being loaded, makes screenshot of whole screen resolution - 1440 x 900 in my case, and happily exits.