Check the setup
- Python 3.10 or newer
- A notebook environment such as Jupyter or Google Colab
- An internet connection
Consolidated UK legislation in machine-readable form for building statute lookup, citation, and change-monitoring tools.
From source to product signal
legislation.gov.uk publishes consolidated UK Acts and statutory instruments through a documented HTTP API. Start with one well-known Act and its XML representation. The service is free to reuse commercially under OGL v3.0; some EU-derived items carry an additional Commission reuse notice.
Install the packages, then run the notebook cell.
python -m pip install pandas requests
import xml.etree.ElementTree as ET
import pandas as pd
import requests
response = requests.get(
"https://www.legislation.gov.uk/ukpga/2018/12/data.xml",
timeout=30,
)
response.raise_for_status()
root = ET.fromstring(response.content)
ns = {"a": "http://www.legislation.gov.uk/namespaces/legislation"}
title = root.findtext(".//a:Title", default="", namespaces=ns)
sections = [
{
"number": item.findtext("a:Number", default="", namespaces=ns),
"title": item.findtext("a:Title", default="", namespaces=ns),
}
for item in root.findall(".//a:P1group", ns)[:20]
]
print(title)
print(pd.DataFrame(sections).head())Test a useful signal
Test whether legislation.gov.uk URIs can power a bounded statute browser.
The National Archives is a government source. Last verified 2026-08-18. Temporal coverage: historic and in-force UK legislation.