import requests
import json


def getUrl(articleName, fqdn='https://en.wikipedia.org/', apiPath='w/api.php', exceptNull=False):
    """Uses the WikiMedia API to determine the ID of a page with the given
       title, which is then used to construct a stable URL for the corresponding
       page.
    """
    queryString = '?action=query&prop=info&format=json&titles=' + articleName.replace(' ', '%20')
    res = requests.get(fqdn + apiPath + queryString)
    data = json.loads(res.content)
    pages = data['query']['pages']
    keys = list(pages.keys())
    id = int(keys[0])
    if id < 0 and exceptNull:
        raise Exception('Null page returned for article name "%s"' % articleName)
    return '%s?curid=%u' % (fqdn, id)