Skip to content

Instantly share code, notes, and snippets.

@mrichman
Created February 16, 2017 15:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrichman/a9641981cec9b62004980bcf6a3cc7fc to your computer and use it in GitHub Desktop.
Save mrichman/a9641981cec9b62004980bcf6a3cc7fc to your computer and use it in GitHub Desktop.
Download a website screenshot using Google's PageSpeed API
#!/bin/env python
""" Download a website screenshot using Google's PageSpeed API """
import base64
import requests
site = "https://twitter.com/mrichman"
# api = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?screenshot=true&strategy=mobile"
api = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?screenshot=true&strategy=desktop"
r = requests.get(api, [('url', site)])
site_data = r.json()
screenshot_encoded = site_data['screenshot']['data']
# Google has a weird way of encoding the Base64 data
screenshot_encoded = screenshot_encoded.replace("_", "/")
screenshot_encoded = screenshot_encoded.replace("-", "+")
screenshot_decoded = base64.b64decode(screenshot_encoded)
with open('screenshot.jpg', 'wb') as f:
f.write(screenshot_decoded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment