Skip to content
Advertisement

How to automate Google PageSpeed Insights tests using Python

Is there a way to automate checking Google Page Speed scores?

Advertisement

Answer

So I figured out how to do this using the Google Page Speed API buried in the documentation.

The TL:DR explanation is you can use the following URL setup, replacing the bracketed values after enabling Google Pagespeed API in Cloud Console (and if in a browser you must also have Authenticated Google Cloud User Signed In).

JavaScript

As you can see from the link above you will need a Google Pagespeed API Key. These are from scratch setup instructions. If your project is already on Cloud Console you can skip the first few steps.

  1. Go to Cloud https://console.developers.google.com
  2. Sign up for an account if necessary.
  3. Create a Project
  4. Go to Menu > API Manager > Credentials
  5. Create credentials button > API Key
  6. Copy Key then hit close
  7. Menu > Dashboard > Enable API
  8. Use Search to find PageSpeed insights API and click on it
  9. Use the “|> ENABLE” button near the title

Once you have an API Key you can replace the values in the URL, it should look something like this:

JavaScript

The parameter strategy=desktop in the url can be changed to strategy=mobile. For mobile you get a speed and a usability score. Here’s what the start of the JSON looks like:

JavaScript

So I automated this using a Python & Python Unit Test.

JavaScript

There is one thing that’s a little of a mystery to me is why browsers need to be authenticated with Google to get JSON from URL but Python requests does not.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement