webshot()

Take a screenshot or PDF of a web page.

Usage

Source

webshot(
    url,
    file="webshot.png",
    *,
    vwidth=992,
    vheight=744,
    selector=None,
    cliprect=None,
    expand=0,
    delay=0.2,
    zoom=1,
    useragent=None,
    page_size="letter",
    margins=0.5,
    landscape=False,
    print_background=False
)

Parameters

url: str | Path

URL to capture. Can be an http/https URL, a file:// URL, or a local file path (automatically converted to a file:// URL).

file: str | Path = "webshot.png"

Output file path. The format is determined by extension: .png, .jpg/.jpeg, .webp for images; .pdf for PDF.

vwidth: int = 992

Viewport width in pixels.

vheight: int = 744

Viewport height in pixels.

selector: str | list[str] | None = None

CSS selector(s) to capture. For images, the screenshot is cropped to the element’s bounding box. For PDFs, produces an element-bounded PDF sized to fit the element. Mutually exclusive with cliprect=.

cliprect: tuple[float, float, float, float] | None = None

Explicit clip rectangle as (x, y, width, height) in CSS pixels. Mutually exclusive with selector=.

expand: int | tuple[int, int, int, int] = 0

Pixels to expand around the selector bounding box. Single int for all sides, or (top, right, bottom, left) tuple.

delay: float = 0.2

Seconds to wait after page load before capturing.

zoom: float = 1

Zoom/scale factor for raster images (PNG, JPEG, WebP). Values > 1 produce higher resolution images. Ignored for PDF output since PDFs are vector format and always render at full resolution.

useragent: str | None = None

Custom User-Agent string.

page_size: PaperSize = "letter"

Paper size for PDF output (e.g., "letter", "a4").

margins: float | tuple[float, float, float, float] = 0.5

Margins in inches for PDF output. Single float or 4-tuple.

landscape: bool = False

Whether to use landscape orientation for PDF output.

print_background: bool = False
Whether to print CSS backgrounds in PDF output.

Returns

Path
The absolute path to the output file.