in Uncategorized

Creating website screenshots with Python and pyppeteer

pyppeteer is a Python port of a headless Chromium browser automation library puppeteer. It is a very useful tool in general and can be used to perform a number of tasks like web scraping, automation and making website screenshots! The advantage of using an actual browser to create website screenshots is clear: it gives us the very same result people will see in their own browsers.

Once the screenshot is taken, we will use Python imagining library Pillow to resize the resulting image to the size we want. We can call our code in number of ways, but for this example I will use CLI library Click. You can learn more about Click in my other blog post Building command-line interfaces in Python.

So let’s begin by installing our dependencies:

Pyppeteer will also need a working copy of Chromium, which can be downloaded directly by invoking pyppeteer-install command which will become available after installation.

Once this is done, we can start creating our program. The main code will start a headless Chromium, navigate to a page we want to capture and save its screenshot to a file of our choosing. The interesting thing is that pyppeteer has asynchronous API, so we will define our function also as asynchronous and later on use Python’s AsyncIO event loop to run it:

The code is very straightforward due to the pyppeteer’s simple API. The important decision here is that we want to be able to provide browser’s viewport dimensions. Unless set to a proper screen size, Chromium would start with a very small window and the screenshot would be useless.

After the screenshot is saved, we want to resize it to a size for publishing, so for that let’s implement a resize_screenshot function:

To tie it all together, we will use Click to implement a command. This command will allow us to change all of the parameters we need: the URL, viewport size, final screenshot size and the filenames used for saving the images:

As our capture_screenshot function is async, we had to create AsyncIO event loop to invoke it. You can see the whole working example including the imports in my python-screenshots repository.

To run the program, simply invoke our screenshot command:

And this is all! By default, screenshot.png and screenshot_resized.png files will be saved in the project directory for further processing.

Loading Likes...

Check out my upcoming book Efficient developer on software development.