Last active
August 29, 2015 14:10
-
-
Save jywsn/7cdbf51a8107d6635542 to your computer and use it in GitHub Desktop.
screenshots - python, capturejs, phantomjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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