Marketing

How to Automate Website Screenshots [TECH GUIDE]

Post by
Andrew Pierno
How to Automate Website Screenshots [TECH GUIDE]

In our previous post, we showed how you can take screenshots using Python, whether it’s for web scraping, monitoring user or competitor behavior, or any other use case.

But what if you want to take many screenshots automatically? In this post, we’ll show you how to quickly and easily automate website screenshots using ScreenshotAPI.

Preview: 3 Key Methods to Take Programmatic Screenshots

  • Using a list for programmatic screenshots
  • Using a text file to take automated screenshots
  • Taking time-based programmatic screenshots

Let’s dive in!

First, Why Do You Want to Automate Screenshots?

Before you choose an automated screenshot tool, know your why. Your reason for needing automated screenshots will dictate the functionality you need.

● Do you want to track and monitor website behavior as a developer? You’ll need a solution that’s integrated with your existing stack.

● Monitoring employee activity? Focus on privacy.

● Automating workflows when working with different dashboards? Focus on integrations.

● Keeping track of designs? Look for HD screenshots.

● Sharing data behind logins with teammates? Privacy comes first!

When it comes to high-quality screenshots you can easily integrate with your existing stack, ScreenshotAPI is your buddy!

 

Automating Website Screenshots With ScreenshotAPI

There are a few simple ways to use ScreenshotAPI as your programmatic screenshot-taking tool. You can always manually request screenshots, but if you’re an advanced user, you’ll love the simple integration.

Once again, we’ll use Python.

Setting Up

 To start, you’ll need to do some preparation, similar to our previous post. So, import the parse and request modules of Python’s urllib package with the following code:

import urllib.parse
import urllib.request

Then import Python’s ssl module with the code:

import ssl

The ssl module gives you access to Transport Layer Security encryption and peer authentication facilities for network sockets. When you’ve imported the ssl module, you can create an unverified SSLContext object, which allows you to access websites without valid SSL certificates:

ssl._create_default_https_context = ssl._create_unverified_context

Next, you’ll set the variables you’ll use to construct the query parameters. First, define the token variable, which is a string containing your ScreenshotAPI API key:

token = “Your API Key”

Then set the width and height of your target render. These are both integers:

width = 1920
height = 1080

Remember, you don’t have to use the same numbers we’ve used here; you can choose those that best meet your needs. You’ll then also define your output variable, a string that specifies the output format, either as an image or in JSON format. In this case, we’ll output images:

output = “image”

You’ll notice that we haven’t set our url variable as we did in our previous post. This is because we’ll do so when we deal with the different methods for automating screenshots.

Method 1: Using a List

The first method, a Python list, is probably the simplest of all the techniques we’ll use. To start, you’ll put all the websites you want to take screenshots of in a Python list. We’ll use a few search engines and news sites in our example.

url_list = [“www.google.com”, “www.yahoo.com”,”www.skynews.com”, “www.cnn.com”, “www.msn.com”]

Next, we’ll iterate through the list using a For loop, and with each iteration, we’ll set the url variable, construct the query parameters, and call the API:

for item in url_list:
url = item
query = "https://shot.screenshotapi.net/screenshot"
query += "?token=%s&url=%s&width=%d&height=%d&output=%s" % (token, url, width, height, output)
urllib.request.urlretrieve(query, "./screenshot.png")

This script will iterate through your entire list and take and save a screenshot for every website on your list.

Method 2: Using a Text File

While the list method mentioned above can be effective, it could become quite cumbersome if you have a lot of websites you want to take screenshots of. 

Don’t worry, you don’t run it separately every single time - we wouldn’t want you falling asleep on us!

Instead, you just use a text file.

Let’s say you have a text file, saved as urllist.txt, with the following content:

www.google.com
www.yahoo.com
www.skynews.com
www.cnn.com
www.msn.com

You'll use the same process described above, but you’ll first need to open the file for Python to read it. To do this, you’ll use the with statement with open and set the mode to r for reading:

with open(“urllist.txt”, ”r”) as url_file:

With the file open, you’ll again use a for loop to iterate through the items in your text file:

for line in url_file:

With each iteration, you’ll set the url variable, strip the end-line break from the line, construct the query parameters, and call the API:

url = line.strip()
query = "https://shot.screenshotapi.net/screenshot"
query += "?token=%s&url=%s&width=%d&height=%d&output=%s" % (token, url, width, height, output)
urllib.request.urlretrieve(query, "./screenshot.png")

This script iterates through all the websites on your list, no matter how many, and takes a screenshot of each.

Method 3: Time Based

The methods described above work great if you want to take bulk screenshots of many sites, but what do you do if you want to take automatic screenshots of only one site? 

This could, for example, be for employee or user monitoring.

One of the simplest ways to do this is using the time module. You’ll need to import the module during the setup stage mentioned earlier. You’ll also set your url variable, and you’ll construct your query during the setup process:

import time
url = “www.google.com”
query = "https://shot.screenshotapi.net/screenshot"
query += "?token=%s&url=%s&width=%d&height=%d&output=%s" % (token, url, width, height, output)

Next, you’ll use a while loop to let the code execute continuously, but with the sleep function to ensure that it only repeats in seconds after a given time period. For example, if you want a screenshot every ten minutes, your code will be:

while(True):
urllib.request.urlretrieve(query, "./screenshot.png")
time.sleep(600)

You can set this time period based on how often you want screenshots to be taken, and the script will then execute and save screenshots at these defined intervals.

Automate Your Screenshots Now With ScreenshotAPI

And that’s it! ScreenshotAPI gives you plenty of ways to programmatically take screenshots (and choose the exact cadence that fits your workflow best). There’s no reason to jump through hoops anymore.

You’re one API key away from getting screenshots off your to-do list! 

Head over to ScreenshotAPI and grab your API key to get started! Plans start from $9 per month for 1,000 screenshots.