Get the archived version of a page closest to a particular date

New to Jupyter notebooks? Try Using Jupyter notebooks for a quick introduction.

To get the archived version of a page closest to a particular date we can use the Memento API. Variations in the way Memento is implemented across repositories are documented in Getting data from web archives using Memento. The functions below smooth out these variations to provide a (mostly) consistent interface to the UK Web Archive, Australian Web Archive, New Zealand Web Archive, and the Internet Archive. They could be easily modified to work with other Memento-compliant repositories.

To get information about available Mementos:

query_timegate([timegate], [url], [date], [timezone])

To get a single Memento closest to your target date:

get_memento([timegate], [url], [date], [timezone])


  • timegate – one of 'ukwa' (UK), 'awa' (Australia), 'nzwa' (New Zealand), or 'ia' (Internet Archive)
  • url – the url you want to look for in the archive
  • date – the target date in ISO format, 'YYYY-MM-DD' (optional, will default to most recent date)
  • tz – a timezone string for your local timezone (optional)
In [1]:
import arrow
import requests
In [2]:
# These are the repositories we'll be using
    "awa": "",
    "nzwa": "",
    "ukwa": "",
    "ia": "",

def format_date_for_headers(iso_date, tz):
    Convert an ISO date (YYYY-MM-DD) to a datetime at noon in the specified timezone.
    Convert the datetime to UTC and format as required by Accet-Datetime headers:
    eg Fri, 23 Mar 2007 01:00:00 GMT
    local = arrow.get(f"{iso_date} 12:00:00 {tz}", "YYYY-MM-DD HH:mm:ss ZZZ")
    gmt ="utc")
    return f'{gmt.format("ddd, DD MMM YYYY HH:mm:ss")} GMT'

def parse_links_from_headers(response):
    Extract Memento links from 'Link' header.
    links = response.links
    return {k: v["url"] for k, v in links.items()}

def query_timegate(timegate, url, date=None, tz="Australia/Canberra"):
    Query the specified repository for a Memento.
    headers = {}
    if date:
        formatted_date = format_date_for_headers(date, tz)
        headers["Accept-Datetime"] = formatted_date
    elif not date:
        formatted_date = format_date_for_headers(
            arrow.utcnow().format("YYYY-MM-DD"), tz
        headers["Accept-Datetime"] = formatted_date
    # Note that you don't get a timegate response if you leave off the trailing slash, but extras don't hurt!
    tg_url = (
        if not url.endswith("/")
        else f"{TIMEGATES[timegate]}{url}"
    # print(tg_url)
    # IA only works if redirects are followed -- this defaults to False with HEAD requests...
    if timegate == "ia":
        allow_redirects = True
        allow_redirects = False
    response = requests.head(tg_url, headers=headers, allow_redirects=allow_redirects)
    return parse_links_from_headers(response)

def get_memento(timegate, url, date=None, tz="Australia/Canberra"):
    If there's no memento in the results, look for an alternative.
    links = query_timegate(timegate, url, date, tz)
    # NLNZ doesn't always seem to return a Memento, so we'll build in some fuzziness
    if links:
        if "memento" in links:
            memento = links["memento"]
        elif "prev memento" in links:
            memento = links["prev memento"]
        elif "next memento" in links:
            memento = links["next memento"]
        elif "last memento" in links:
            memento = links["last memento"]
        memento = None
    return memento


Query NZWA Timegate for information about the NLNZ home page.

In [3]:
query_timegate("nzwa", "")
{'original': '',
 'timegate': '',
 'timemap': '',
 'memento': ''}

Get a version of my blog from around 2005. First from the AWA:

In [4]:
get_memento("awa", "", "2005-01-01")

Then from the IA:

In [5]:
get_memento("ia", "", "2005-01-01")

Get a recent memento of the British Library site:

In [7]:
get_memento("ukwa", "")

Created by Tim Sherratt for the GLAM Workbench. Support me by becoming a GitHub sponsor!

Work on this notebook was supported by the IIPC Discretionary Funding Programme 2019-2020