Skip to content
shotsmith
Features Docs Guides Pricing Changelog Sign inStart free

How to take website screenshots in Python

Capture website screenshots and PDFs from Python with the standard library, plus Django and FastAPI examples. No Selenium or Playwright needed.

4 min read

Most Python screenshot tutorials start with Selenium or Playwright: install a browser, a driver, and then keep them working on your servers. shotsmith runs Chrome for you, so a screenshot is a single HTTP request, and the examples below use nothing but Python’s standard library.

1. Get an API key

Create a free account (100 renders a month, no card) and copy your access key. Read it from the environment, for example os.environ["SHOTSMITH_ACCESS_KEY"].

2. Take a screenshot

urllib raises HTTPError for any error response, so a failed render never gets written to disk by mistake:

import urllib.request
from urllib.parse import urlencode

params = {
    "access_key": "YOUR_ACCESS_KEY",
    "url": "https://shotsmith.dev/sample",
    "format": "png"
}

url = "https://shotsmith.dev/v1/take?" + urlencode(params, doseq=True)

# Raises urllib.error.HTTPError on a 4xx/5xx response
with urllib.request.urlopen(url, timeout=120) as res, open("capture.png", "wb") as f:
    f.write(res.read())

3. Full page, mobile and clean

import urllib.request
from urllib.parse import urlencode

params = {
    "access_key": "YOUR_ACCESS_KEY",
    "url": "https://shotsmith.dev/sample",
    "full_page": "true",
    "viewport_device": "iphone_15_pro",
    "block_cookie_banners": "true",
    "block_ads": "true",
    "format": "jpg"
}

url = "https://shotsmith.dev/v1/take?" + urlencode(params, doseq=True)

# Raises urllib.error.HTTPError on a 4xx/5xx response
with urllib.request.urlopen(url, timeout=120) as res, open("capture.jpg", "wb") as f:
    f.write(res.read())

Lists, like several hide_selectors, are sent as repeated parameters. That’s what doseq=True does in urlencode.

4. Get the page as Markdown

Set format to markdown to get the readable content of a page, after JavaScript has run, ready for an LLM or a search index:

import urllib.request
from urllib.parse import urlencode

params = {
    "access_key": "YOUR_ACCESS_KEY",
    "url": "https://shotsmith.dev/sample",
    "format": "markdown"
}

url = "https://shotsmith.dev/v1/take?" + urlencode(params, doseq=True)

# Raises urllib.error.HTTPError on a 4xx/5xx response
with urllib.request.urlopen(url, timeout=120) as res, open("capture.md", "wb") as f:
    f.write(res.read())

More on this in turning web pages into Markdown for LLMs.

5. A Django view that returns a PDF

This view renders one of your own pages as an A4 PDF download. If the render fails, it passes on the API’s JSON error and status:

import json
import os
import urllib.request
from urllib.error import HTTPError
from urllib.parse import urlencode

from django.http import HttpResponse, JsonResponse

API = "https://shotsmith.dev/v1/take"


def page_pdf(request, slug):
    """Return /reports/<slug>/ from your own site as a downloadable PDF."""
    params = urlencode({
        "access_key": os.environ["SHOTSMITH_ACCESS_KEY"],
        "url": request.build_absolute_uri(f"/reports/{slug}/"),
        "format": "pdf",
        "pdf_paper_format": "a4",
        "pdf_print_background": "true",
    })
    try:
        with urllib.request.urlopen(f"{API}?{params}", timeout=30) as res:
            pdf = res.read()
    except HTTPError as error:
        # Errors are JSON with a stable error_code and a message saying what to change.
        return JsonResponse(json.loads(error.read() or b"{}"), status=error.code)

    response = HttpResponse(pdf, content_type="application/pdf")
    response["Content-Disposition"] = f'attachment; filename="{slug}.pdf"'
    return response

6. An async FastAPI endpoint

In async code, use an async HTTP client such as httpx so the request doesn’t block your event loop:

import os

import httpx
from fastapi import FastAPI, HTTPException
from fastapi.responses import Response

app = FastAPI()
client = httpx.AsyncClient(base_url="https://shotsmith.dev", timeout=30)


@app.get("/thumbnail")
async def thumbnail(url: str):
    res = await client.get("/v1/take", params={
        "access_key": os.environ["SHOTSMITH_ACCESS_KEY"],
        "url": url,
        "format": "webp",
        "image_width": 640,
        "block_cookie_banners": "true",
        "cache": "true",
    })
    if res.is_error:
        error = res.json()
        raise HTTPException(status_code=res.status_code, detail=error["error_code"])

    return Response(res.content, media_type=res.headers["content-type"])

cache=true makes repeat requests for the same URL instant and free for four hours. Like any endpoint that renders URLs it’s given, this one should only accept sites you expect, because every call uses your quota.

7. Errors and long renders

Errors are JSON with a stable error_code, listed in the error reference, and failed renders aren’t billed. Synchronous requests must finish within 18 seconds. For slow pages, videos or batches, use async rendering with webhooks.