Skip to content

Instantly share code, notes, and snippets.

@jywsn
Last active August 29, 2015 14:10
Show Gist options
  • Save jywsn/7cdbf51a8107d6635542 to your computer and use it in GitHub Desktop.
Save jywsn/7cdbf51a8107d6635542 to your computer and use it in GitHub Desktop.
screenshots - python, capturejs, phantomjs
"""
Install nodejs:
http:http://nodejs.org
On Ubuntu 12.x, you may need to do this:
sudo apt-get update
sudo apt-get install -y python-software-properties
sudo add-apt-repository -y ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install -y nodejs
For running phantomjs:
sudo apt-get install -y libfontconfig # for phantomjs
For 'make', for capturejs:
sudo apt-get install -y build-essential
Install phantomjs:
sudo np install -g phantomjs
Install capturejs:
sudo npm install -g capturejs
"""
import os
import tempfile
import subprocess
page_url = 'the url of the page you want to screenshot'
width = 800
height = 600
# Write screenshot to temp file
temp_dir = tempfile.gettempdir()
temp_name = repr(time.time())+'.png'
temp_path = os.path.join(temp_dir, temp_name)
print 'Writing', temp_path
os.chdir(temp_dir)
subprocess.check_call([
'capturejs',
'-V', '%dx%d' % (width, height),
'-u', page_url,
'-o', temp_name,
'-R', '1000' # delay to allow page to render
])
# Check for silent error
if not os.path.isfile(temp_path):
raise Exception('Error creating screenshot')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment