ScreenAPI

Guides

How to screenshot a website with Python

Take website screenshots and PDFs from Python with the requests library. No Selenium, no browser driver. Full page, mobile, batch and error handling examples.

Selenium and Playwright can take screenshots from Python, but they need a browser and a driver on every machine that runs your script. With an API, requests is all you need, and it works the same on your laptop, a cron job or a cloud function.

Basic example

import os
import requests

r = requests.post(
    "https://getscreenapi.com/api/v1/screenshot",
    headers={"x-api-key": os.environ["SCREENAPI_KEY"]},
    json={"url": "https://example.com", "full_page": True, "format": "png"},
    timeout=90,
)
r.raise_for_status()
with open("example.png", "wb") as f:
    f.write(r.content)

print("credits left:", r.headers.get("X-Credits-Remaining"))

Screenshot a list of pages

import os, time, requests
from urllib.parse import urlparse

URLS = ["https://example.com", "https://www.wikipedia.org"]
session = requests.Session()
session.headers["x-api-key"] = os.environ["SCREENAPI_KEY"]

for url in URLS:
    r = session.post("https://getscreenapi.com/api/v1/screenshot",
                     json={"url": url, "format": "webp", "cache_ttl": 86400}, timeout=90)
    if r.status_code == 429:
        time.sleep(int(r.headers.get("Retry-After", "5")))
        continue
    r.raise_for_status()
    name = urlparse(url).hostname + ".webp"
    open(name, "wb").write(r.content)

With cache_ttl, running the script again within 24 hours costs nothing.

Save a page as PDF

r = requests.post("https://getscreenapi.com/api/v1/screenshot",
                  headers={"x-api-key": os.environ["SCREENAPI_KEY"]},
                  json={"url": "https://example.com", "format": "pdf"}, timeout=90)
open("page.pdf", "wb").write(r.content)

Turn your own HTML into an image

Useful for social cards, certificates or charts generated by your app:

html = "<div style='font:48px sans-serif;padding:40px'>Hello from Python</div>"
r = requests.post("https://getscreenapi.com/api/v1/screenshot",
                  headers={"x-api-key": os.environ["SCREENAPI_KEY"]},
                  json={"html": html, "width": 1200, "height": 630}, timeout=90)

All examples work on the free plan.

Get a free key