

Ask HN: How can I take screenshots of websites programmatically? - wdewind

Hey all,<p>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.<p>I've seen this feature on the new google search results preview and a few other places.<p>Couldn't really find much after googling, has anyone tried to do this before?
======
joeld42
Here's some code for doing this with Qt Webkit for a local app:

[http://labs.qt.nokia.com/2008/11/03/thumbnail-preview-of-
web...](http://labs.qt.nokia.com/2008/11/03/thumbnail-preview-of-web-page/)

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.

------
user24
> Couldn't really find much after googling

[http://www.google.com/search?q=take+screenshots+of+websites+...](http://www.google.com/search?q=take+screenshots+of+websites+programmatically)

<http://www.google.com/search?q=webpage+screenshot+script>

------
shabda
<http://www.paulhammond.org/webkit2png/>

------
staunch
What I've done using Xvnc + Perl:

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.

------
caseyf
I use self-hosted Browsershots with some modifications. With 4 ShotFactories
running, a slow disk, 5 thumbnail sizes, and non-optimal reuse settings (reuse
browser only once, reuse VNC server only once) it creates about 10 thumbnails
per minute.

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)

------
gspyrou
I think this can be done by using the Silverlight WebBrowserBrush control and
the WriteableBitmap class. [http://msdn.microsoft.com/en-
us/library/ff457752(v=VS.95).as...](http://msdn.microsoft.com/en-
us/library/ff457752\(v=VS.95\).aspx) [http://msdn.microsoft.com/en-
us/library/system.windows.media...](http://msdn.microsoft.com/en-
us/library/system.windows.media.imaging.writeablebitmap\(VS.95\).aspx)

------
byoung2
There are services that do this, such as <http://www.shrinktheweb.com/>

------
swanson
You can roll your own with Qt's Webkit module; load the page programmatically
and then render the frame into a QImage and save that as a png/jpg etc.

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.

------
techbio
Sorry to answer with a trivial Google search, but: <http://www.websnapr.com/>
might do something for you.

------
mgkimsal
run a browser in a vm and invoke a screenshot utility.

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

------
joshfraser
if you need screenshots from multiple browsers selenium is the way to go.
saucelabs are great if you don't want to host it yourself.

------
bmelton
There are a couple of web services that offer the exact thing you're looking
for. Websnapr is one, Browsershots is (to my knowledge) the oldest competitor
in the space.

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.

