mobile usability api

Google Mobile-Friendly Test API Tutorial for Python

Estimated Read Time: 4 minute(s)
Common Topics: weight, mobile, api, friendly, test

This post explains how to use Google’s Mobile-Friendly Test API with Python. The Mobile-Friendly Test indicates whether a page is mobile-friendly, reports resources blocked to Google, and returns a screenshot of the page.

Because the Mobile-Friendly Test uses the Googlebot Smartphone user agent, it not only checks mobile-friendliness but also shows how Google views a page and whether any resources are blocked.

Using the Mobile-Friendly Test API with Python makes it practical to analyze many pages in bulk and verify how Googlebot sees them. If you change your site’s layout, theme, or how pages are served to Googlebot, the API lets you quickly check many pages to ensure Googlebot renders them as expected by examining blocked resources and screenshots.

Requirements and Assumptions

Import Modules

This tutorial uses three Python modules to interact with the Mobile-Friendly Test API:

  • requests – to make POST requests to the Mobile-Friendly Test API endpoint.
  • json – to parse and navigate the JSON response.
  • base64 – to decode screenshots returned encoded in base64.

Install the requests module if needed:

pip install requests

With that installed, let’s see how the API works.

Making the request to the API endpoint

The endpoint is https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run. It accepts the following parameters:

  • URL: the URL to test.
  • requestScreenshot: whether to return a screenshot. Default is false; set to “true” to request a screenshot.
  • key: the Google API key set up previously.
url = 'https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run'
params = {

            'url': 'https://www.danielherediamejias.com',

            'requestScreenshot': 'true',

            'key': "yourkey"

        }

x = requests.post(url, data = params)
data = json.loads(x.text)

The code above sends a request to the Mobile-Friendly Test API for https://www.danielherediamejias.com. The response is parsed using json.loads().

Which information can we get now?

  • Response Code: verify the response code is 200; other codes may indicate Googlebot Smartphone had trouble crawling the page.
  • Status: whether the audit completed.
  • Mobile friendliness verdict: if the page is mobile friendly the API returns “MOBILE_FRIENDLY”. If there are mobile usability issues, the API lists them.
  • Blocked resources: if any resources are blocked, confirm these do not prevent Googlebot Smartphone from rendering the page correctly.
  • Screenshot: you can obtain a screenshot showing how Googlebot Smartphone sees the page.

The keys or expressions used to access these fields are:

  • Response code: str(x)[len(str(x))-5:len(str(x))-2]
  • Status: data[“testStatus”][“status”]
  • Mobile Friendliness: data[“mobileFriendliness”]. If there are mobile-friendliness issues, get the list of issues with:
listmobileissues = []

for iteration in range (len(data["mobileFriendlyIssues"])):
     listmobileissues.append(data["mobileFriendlyIssues"][iteration]["rule"])
  • Blocked Resources List:
listblockedresources = []

for iteration in range (len(data["resourceIssues"])):
     listblockedresources.append(data["resourceIssues"][iteration]["blockedResource"]["url"])
  • Screenshot: data[“screenshot”][“data”]

Putting everything together

Combining the pieces, the script looks like this:

import requests
import json
import base64


try:
    url = 'https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run'
    params = {
            'url': 'yoururl',
            'requestScreenshot': 'true',
            'key': "yourkey"
        }
    x = requests.post(url, data = params)
    data = json.loads(x.text)  
    with open("Finalscreenshot.png", "wb") as fh:
        fh.write(base64.b64decode(data["screenshot"]["data"]))
        print("Response code for Google Smartphone is " + str(x)[len(str(x))-5:len(str(x))-2])
        print("Page is " + data["mobileFriendliness"])
        
        if data["mobileFriendliness"] == "NOT_MOBILE_FRIENDLY":
            for iteration in range (len(data["mobileFriendlyIssues"])):
                print("The page has problems with " + str(data["mobileFriendlyIssues"][iteration]["rule"]))
        
        issues = data.get("resourceIssues", 0)
        if issues != 0:
            for iteration in range (len(data["resourceIssues"])):
                print("Problems with the blocked resources " + str(data["resourceIssues"][iteration]["blockedResource"]["url"]))
 
        print("Screenshot from " + str(params["url"]) + " is taken and downloaded.")
except:
    print("Problem with " + str(params["url"]) + ". " + str(x)[len(str(x))-5:len(str(x))-2] + " Response Code.")

The output from this script looks like the example below:

The script also downloads the screenshot as a PNG. For this post, the screenshot returned was:

If the page has issues, the script prints the response code. Running the script against a non-mobile-friendly page might produce output like this:

That’s all — I hope you found this post useful!

Google Mobile Friendly Test API FAQ

How can I integrate Google’s Mobile Friendly Test Tool API with Python?

Use Python scripts to interact with Google’s Mobile Friendly Test Tool API, allowing you to programmatically check the mobile-friendliness of a URL.

What Python libraries are commonly used for making API requests to Google’s Mobile Friendly Test Tool?

The requests library is commonly used in Python for making HTTP requests. It can be employed to send requests to Google’s Mobile Friendly Test Tool API and retrieve mobile-friendliness information for a specific URL.

What information can I obtain from Google’s Mobile Friendly Test Tool API with Python?

Python scripts can retrieve information about the mobile-friendliness status of a URL, including whether the page is mobile-friendly, details about mobile usability issues, and suggestions for improvement.

Are there any authentication requirements when using Google’s Mobile Friendly Test Tool API with Python?

No authentication is typically required for accessing Google’s Mobile Friendly Test Tool API. It is publicly accessible, allowing Python scripts to query the API without the need for authentication.

Where can I find examples and documentation for using Python with Google’s Mobile Friendly Test Tool API?

Refer to the official documentation for Google’s Mobile Friendly Test Tool API for detailed guides and examples. Explore online tutorials and Python resources for practical demonstrations and implementation details.

Daniel Heredia
Latest posts by Daniel Heredia (see all)