Web scraping. Monitoring user behavior. Monitoring competitor behavior. You’ll often find yourself needing to take automated website screenshots. The problem is, we’re using old solutions for new challenges.
Today, we’ll look at the traditional way to take screenshots using Python and a much simpler alternative with ScreenshotAPI.
Let’s take a look!
If you want to take screenshots the traditional Python way, you’ll first need to import the pyautogui module:
pip install pyautogui
Once you’ve imported the package, decide where you want the screenshot to be saved.
For example, let’s use the following location:
C:\Users\User\Desktop\Screenshots\screenshot.png
You’ll call the screenshot function to take a screenshot, save it as a variable, and then use the save function to save the screenshot in the location mentioned earlier:
exampleScreenshot = pyautogui.screenshot()
exampleScreenshot.save(r'C:\Users\User\Desktop\Screenshots\screenshot.png')
The problem with this method is that you’ll need to navigate every webpage you want a screenshot of manually. This is quite a hassle, so ScreenshotAPI takes a different approach.
With ScreenshotAPI, you can fully automate taking screenshots. Regardless of the format (from PNG to PDF) or the volume, you’ll get high-quality screenshots every time.
When taking screenshots with ScreenshotAPI, just import the parse and request modules of Python’s urllib package. Use the following code:
import urllib.parse
import urllib.request
Then, import Python’s ssl module that gives you access to Transport Layer Security (also known as TLS, Secure Sockets Layer, or SSL) encryption and peer authentication facilities for network sockets. Here, you’ll use:
import ssl
Next, create an unverified SSLContext object with the ssl._create_unverified_context() function:
ssl._create_default_https_context = ssl._create_unverified_context
Set the variables that you’ll use to construct your query parameters, including:
● The string containing your URL key
token = "Your ScreenshotAPI API Key"
● The encoded URL string containing the URL you’re targeting.
url = urllib.parse.quote_plus(" https://google.com ")
● The integer indicating the width of your target render.
width = 1920
● The integer indicating the height of your target render.
height = 1080
● The string specifying the output format, “image” or “json”.
output = "image"
Once you’ve set the variables, you can construct your query parameters and URL with the following code:
query = "https://shot.screenshotapi.net/screenshot"
query += "?token=%s&url=%s&width=%d&height=%d&output=%s" % (token, url, width, height, output)
Using the query parameters above, you can then call the API:
urllib.request.urlretrieve(query, "./screenshot.png")
For every screenshot you want to take, you’ll then only need to change the URL variable to get screenshots of different web pages.
It’s time to turn taking website screenshots using Python into a set it & forget it affair. Grab your ScreenshotAPI key and get started!