Tutorial: Simple web accessibility scores using Google Lighthouse and Python
Web accessibility standards ensure that anyone — regardless of ability — can use your website.
Accessibility principles create a better user experience. Proper captions, color contrast, and alternative text benefit everyone.
The standards are also frequently required by law. In the United States, state and local governments and businesses that are open to the public (like retail stores and banks) must meet Americans with Disabilities Act (ADA) standards for web pages.
Google Lighthouse is a tool that allows you to audit web pages for accessibility, SEO, performance, and more. In this blog post, we'll explore how to use Lighthouse and Python to calculate simple accessibility scores for a list of web pages and store the data as a DataFrame.
First, get an API key:
- Go to Google Cloud Console.
- Create a new project.
- Enable the PageSpeed Insights API.
- Create API credentials → API key.
Next, consider this Python code:
import requests
import pandas as pd
def get_lighthouse_accessibility_score(url):
api_url = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed"
params = {
"url": url,
"category": "accessibility",
"strategy": "desktop",
"key": "YOUR_KEY_HERE"
}
response = requests.get(api_url, params=params)
data = response.json()
score = data['lighthouseResult']['categories']['accessibility']
return score
urls = ["https://framingdata.brianfanney.com/tutorial-use-python-to-store-google-crux-data/",
"https://framingdata.brianfanney.com/tutorial-use-google-sheets-to-store-python-dataframes/"]
df = pd.DataFrame(columns=['url', 'accessibility_data', 'accessibility_score'])
for url in urls:
accessibility_data = get_lighthouse_accessibility_score(url)
print (accessibility_data)
accessibility_score = accessibility_data['score']
print (accessibility_score)
temp_df = pd.DataFrame({
'url': [url],
'accessibility_data': [accessibility_data],
'accessibility_score': [accessibility_score]
})
# Concatenate the current DataFrame with the new row
df = pd.concat([df, temp_df], ignore_index=True)
print (df)
For a given list of URLs, you'll get comprehensive data from Google Lighthouse about each page's accessibility issues as well as an overall page score. At the end of the loop, you'll get a DataFrame with columns for url, raw data, and the accessibility score.
Check out my Python and Google Sheets tutorial for an easy way to store this data. Consider setting up a Google Sheet tab to allow users to specify the URL list. Using the tutorial, you can modify my code above. For example:
input_gsheet = GoogleSheets("YOUR_SHEET_ID","Input",google_sheets_json_key)
input = input_gsheet.getGoogleSheet()
urls = input["URL"].tolist()
Comments