import nokap
nokap.webshot("https://example.com", "example.png")Quick Start
This guide walks you through taking your first screenshot with nokap in under a minute. We’ll cover the three main input modes (capturing a live URL, a local HTML file, and raw HTML strings) so you can choose the approach that fits your workflow. Each example is self-contained and ready to copy into a script or notebook.
Your First Screenshot
The simplest use of nokap is capturing a web page as a PNG:
This launches headless Chrome (if not already running), navigates to the URL, captures the full page, and saves it as a PNG file.
Screenshot a Local HTML File
You can also capture local HTML files:
nokap.webshot("page.html", "page.png")nokap automatically converts local paths to file:// URLs.
From an HTML String
The from_html() function is designed for rendering HTML content directly, which is great for libraries that generate HTML:
html = """
<html>
<body style="font-family: system-ui; padding: 2rem;">
<h1>Hello, nokap!</h1>
<p>This is rendered from a string.</p>
</body>
</html>
"""
nokap.from_html(html, "hello.png")Capture a Specific Element
Use a CSS selector to capture just one element:
nokap.webshot(
"https://example.com",
"heading.png",
selector="h1"
)Generate a PDF
Change the output extension to .pdf:
nokap.webshot("https://example.com", "page.pdf")PDF options like page size, margins, and orientation are available:
nokap.webshot(
"https://example.com",
"page.pdf",
page_size="a4",
landscape=True,
print_background=True,
)Retina Quality
Use zoom for higher resolution output:
# 2× resolution (retina)
nokap.webshot("https://example.com", "retina.png", zoom=2)Using the CLI
nokap also provides a command-line interface:
# Screenshot a URL
nokap webshot https://example.com screenshot.png
# Capture a specific element with 2× zoom
nokap webshot https://example.com header.png -s "h1" -z 2
# Render an HTML file
nokap from-html report.html report.png --selector "table" --expand 10Cleanup
Chrome runs in the background for reuse. Clean up explicitly when done:
nokap.close()This is called automatically at process exit, but explicit cleanup is good practice in long-running applications.
Next Steps
You’ve seen the essentials: capturing URLs, local files, and HTML strings as images or PDFs, targeting specific elements with selectors, and using the CLI for shell-based workflows. These building blocks cover the majority of day-to-day use cases.
- Screenshots: Deep dive into image capture options
- PDF Generation: Full PDF configuration
- The CLI: Command-line usage reference
Each of the guides above expands on what you’ve learned here, covering every parameter in detail. Start with whichever matches your immediate need. (You don’t need to read them in order.)