How to take website screenshots in Python

Posted December 22, 2022 by Dmytro Krasun ‐ 4 min read

Today, there are many options to make screenshots of any URL with Python. Let's examine them all and choose which suits you best.

To take screenshots of a given URL in Python, you can use the following:

  1. Selenium with a web driver of choice.
  2. pyppeteer—a Python port of the JavaScript (headless) Chrome/Chromium browser automation library.
  3. URL to Screenshot API.

Let’s quickly give an example for each option and assess which one is better to use and when.

Selenium in Python

Selenium is a library for automating web browsers. It allows you to control a web browser, such as Google Chrome, Firefox, or Safari, from Python.

Selenium provides a way to write scripts in Python that can interact with web pages in a way that simulates a user interacting with the web page through a web browser. It is useful for a variety of tasks, such as testing web applications, automating boring and repetitive tasks, taking screenshots and web scraping.

Selenium requires a web driver to control a web browser through Python. Some popular web drivers include chromedriver for Google Chrome, geckodriver for Mozilla Firefox, and safaridriver for Safari.

Install Selenium and your driver of choice using pip:

pip install selenium
pip install chromedriver # or geckodriver, or safaridriver

And now let’s write a simple script to take a screenshot in Python:

from selenium import webdriver

# 1. create a web driver instance
driver = webdriver.Chrome()

# 2. navigate to the website
driver.get("https://magician.design/")

# 3. save a screenshot of the current page
driver.save_screenshot("magician.design.png")

# 4. close the web driver
driver.quit()

The result is:

A screenshot of Magician

Use Selenium if you want to take screenshots from a different browser, but keep in mind that its screenshot capabilities are minimal. If you are OK with using Chrome or Chromium and don’t care about other browsers but need a powerful library to take screenshots, check out pyppeteer.

pyppeteer

pyppeteer is a Python port of the JavaScript (headless) Chrome/Chromium browser automation library called Puppeteer.

It is a very powerful library and it can do mostly everything that a browser can do:

  • To crawl a single-page application or generate server-side rendered content.
  • To take screenshots and create PDF documents of web pages.
  • To automate tasks such as form submission, user interface testing, and keyboard input.
  • To set up an automated testing environment using current JavaScript and browser capabilities.
  • To record a timeline trace of your website to troubleshoot performance issues.
  • To automate testing of Chrome extensions.

Install with pip from PyPI (pyppeteer requires Python >= 3.6):

pip install pyppeteer

Let’s open a webpsite and take a screenshot of it:

import asyncio
from pyppeteer import launch

async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.goto('https://softstart.app/')
    await page.screenshot({'path': 'softstart.png'})
    await browser.close()

asyncio.get_event_loop().run_until_complete(main())

The screenshot is stored at softstart.png. That’s it and that’s how simple it is:

A screenshot of Softstart

URL to Screenshot API

One of the most significant downsides of taking screenshots with Selenium or pyppeteer is handling infrastructure and taking care of browsers. They are resource intensive. You might encounter crashes, memory leaks, and high levels of CPU consumption.

So, if you need to take 5-10 screenshots and not that often, I would go with Selenium or pyppeteer. But if you need a scale or don’t want to deal with infrastructure issues, I would use a third-party API, like ScreenshotOne—an URL to screenshot API.

It is free to get started and supports various options—like removing chat widgets, blocking cookie banners, rendering in dark mode, and even animations.

Sign up to get your API access and secret key, and let’s try ScreenshotOne’s official SDK for Python:

pip install screenshotone

And:

import shutil
from screenshotone import Client, TakeOptions

# create API client 
client = Client('<your access key>', '<your secret key>')

# set up options
options = (TakeOptions.url('https://screenshotone.com')
    .format("png")
    .viewport_width(1024)
    .viewport_height(768)
    .block_cookie_banners(True)
    .block_chats(True))

# or render a screenshot and download the image as stream
image = client.take(options)

# store the screenshot the example.png file
with open('screenshotone.png', 'wb') as result_file:
    shutil.copyfileobj(image, result_file)

That’s it. The result is:

A screenshot of ScreenshotOne

That’s how simple it was!

Summary

Let’s quickly summarize the options we considered to render HTML or take screenshots in Python and examine them.

If you already have Selenium in your stack or plan to write automation tests, it might be an excellent choice. Selenium provides support for many browsers.

If you don’t care about browsers and are OK with using Chrome or Chromium, I would go with pyppeteer.

But if you want to save time and energy and don’t want to deal with browser issues, I would give a try URL to screenshot API like ScreenshotOne.

Have a nice day!