Ask HN: How can I take screenshots of websites programmatically?
Hey all,
I'm trying to figure out how to take a screenshot of a site using some kind of script. Ie: give it http://www.google.com, get back a .png/.jpg of google.com.
I've seen this feature on the new google search results preview and a few other places.
Couldn't really find much after googling, has anyone tried to do this before?
12 comments
[ 4.2 ms ] story [ 43.8 ms ] threadrun a browser in a vm and script a 'print to pdf'
run a browser in an X session and do a screen grab of X.
http://www.google.com/search?q=take+screenshots+of+websites+...
http://www.google.com/search?q=webpage+screenshot+script
I tried using Browsershots years ago, and found it really quite slow, so I instead implemented a local version instead. If you're using Linux, you can programmatically launch a browser, take an X-Screenshot, and run that through ImageMagick to crop out browser chrome and OS things.
Offhandedly, I don't know how you'd go about splicing together multiple screenshots to get the long tail of the page, but I'm sure it's doable too, with thought.
That said, websnapr looks faster than I remember browsershots being, and not knowing your requirements, it may be good enough anyway.
It runs headless and I only had to interact with the VNC server years ago when I originally set it up - so that I could configure Firefox with some addons.
(edit: maybe I should have clarified: I only use the Browsershots code, not the service)
1) Run an Xvnc session.
2) Run a browser inside the xvnc session DISPLAY=:2 /usr/bin/firefox http://url.com/ or whatever.
3) Connect to the vnc session with Net::VNC (vnccapture has example code) and take a screenshot.
4) Crop out the browser UI with Image::Imlib2
I needed to run a full browser because I wanted Flash elements to load. I had something like 20 xvnc/browser instances running simultaneously, which was more than enough for me. It could scale indefinitely though.
You can use the Python bindings and hack something together in under 50 lines of code. Hook it up to a message queue or some kind of cron job and you're good to go.
http://labs.qt.nokia.com/2008/11/03/thumbnail-preview-of-web...
What you'd want to do is make a little server that does the same thing but can serve the resulting image, or just a simple utility that makes the thumbnails on demand and just saves them as images and call that from your web app.