import urllib
import urllib3

url = 'https://api.ordnancesurvey.co.uk/opennames/v1/find?'
values = {'query': '''9 Louisa Oakes Close,
         London,
         Greater London,
         E4 8AS England''',
         'key': 'wLFnRmX8mmjwob5ptbnkjaZfuNVKN3WA'}
data = urllib.parse.urlencode(values)

http = urllib3.PoolManager()

try:
    f = http.request('GET', url, fields=values)
except urllib3.exceptions.HTTPError as e:
    if e.code == 401:
        print('401 not authorized')
    elif e.code == 404:
        print('404 not found')
    elif e.code == 503:
        print('service unavailable')
    else:
        print('unknown error: ')
else:
    print('success')
    response = f.data.decode('utf-8')
    print('RESPONSE:', response)

    for line in response.splitlines():
        word_lst = line.split(':')
        for word in word_lst:
            if '"NAME1" ' in word:
                print(line)
            if 'GEOMETRY' in word:
                print(line)
            if 'ID' in word:
                print(line)
            f.release_conn()
