browshot.py

The python library can be installed from PyPI.

sudo pip install -U simplejson browshot

The source code is available on GitHub.

Here are a couple of basic code samples. You can find more code samples for each API call on the API Documentation page.

Download a screenshot (simple API)

# -*- coding: utf-8 -*-
#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################

from browshot import BrowshotClient

browshot = BrowshotClient('my_api_key')


data = browshot.simple('http://mobilito.net/', { 'cache': 60 * 60 * 24 * 365, 'instance_id': 12 }) # 1 year cache, free screenshot
# code above is blocking, it will return when the screenshot finished or failed

if int(data['code']) == 200: # success
  image = open("screenshot.png", mode='wb')
  image.write(data['png'])
  image.close()
  
  print "Screenshot saved to screenshot.png"
else:
  print "Screenshot failed!"
  # the reason for the error is sent as part of the HTTP response in the X-Error header but it is not exposed by this library

# quicker way to save a screenshot
info = browshot.simple_file('http://mobilito.net/', "mobilito.png", { 'cache': 0, 'instance_id': 65, 'screen_width': 1280, 'screen_height': 1024 }) # no cache, premium browser
if info['file'] != '':
  print "Screenshot saved to %s" % info['file']
else:
  print "The screenshot failed"

Download code sample

Download a screenshot (full API)

# -*- coding: utf-8 -*-
#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################

from browshot import BrowshotClient
import time

browshot = BrowshotClient('my_api_key')

screenshot = browshot.screenshot_create('http://www.google.com/', { 'instance_id': 12, 'size': 'page' }) # all default parameters, instance_id = 12 (free)
# If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
while screenshot['status'] != 'finished' and screenshot['status'] != 'error':
  print "Wait...";
  time.sleep(10)
  screenshot = browshot.screenshot_info(screenshot['id'])

# screenshot is done: finished (sucessful) or error (failed)
if screenshot['status'] == 'error':
  print  "Screenshot failed: " + screenshot['error'] # display the reason for the error
else: # request the thumbnail
  image = browshot.screenshot_thumbnail(screenshot['id'])
  
  # save the screenshot
  fp = open("browshot.png", mode='wb')
  fp.write(image)
  fp.close()

Download code sample

Download a 640x480 thumbnail (simple API)

# -*- coding: utf-8 -*-
#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################

from browshot import BrowshotClient

browshot = BrowshotClient('my_api_key')


info = browshot.simple_file('http://www.google.com/', 'google-640.png', { 'width': 640, 'height': 480 })

if info['file'] == '' or info['code'] != 200:
  print "Screenshot failed"
else:
  print "Thumbnail saved at %s" % info['file']

Download code sample

Download a 640x480 thumbnail (full API)

# -*- coding: utf-8 -*-

from browshot import BrowshotClient
import time

browshot = BrowshotClient('my_api_key')


screenshot = browshot.screenshot_create('http://www.google.com/', { 'instance_id': 12, 'size': 'screen' }) # all default paramters, instance_id = 12 (free)

# If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
while screenshot['status'] != 'finished' and screenshot['status'] != 'error':
  print "Wait..."
  time.sleep(10)
  screenshot = browshot.screenshot_info(screenshot['id'])

# screenshot is done: finished (sucessful) or error (failed)
if screenshot['status'] == 'error':
  print  "Screenshot failed: " + screenshot['error'] # display the reason for the error
else: # request the thumbnail
  browshot.screenshot_thumbnail_file(screenshot['id'], '/tmp/google-640.png', { 'width': 640, 'height': 480 })
  print "Screenshot was saved to /tmp/google-640.png"

Download code sample

Try it for free

close

sign up for free account

Already have an account?

no credit card required

About Us

Browshot is a web service to create real time web screenshots in a multitude of virtual devices, including mobile devices like the iPhone 3 & 4, iPad, Android Nexus, etc.

You can use the web dashboard, or our full-featured API.

  • Real time screenshots

  • 15+ mobile devices: iPhone, iPad, Android, etc.

  • 30+ desktop resolutions

  • Fast and reliable

  • Thumbnails of any size, any ratio

  • Full API, open-source libraries

Share