Skip to content

Instantly share code, notes, and snippets.

@hitecherik
Created November 7, 2015 14:58
Show Gist options
  • Save hitecherik/5e5cc1778fb906cdcae6 to your computer and use it in GitHub Desktop.
Save hitecherik/5e5cc1778fb906cdcae6 to your computer and use it in GitHub Desktop.
Retrieve a UK postcode and return a constituency.
import urllib.request
import json
postcode = input("Please enter your postcode: ")
url = "http://uk-postcodes.com/postcode/" + postcode + ".json"
request = urllib.request.Request(url, headers={"User-Agent": "I'm not a bot"})
content = urllib.request.urlopen(request).read().decode()
results = json.loads(content)
print(results["administrative"]["constituency"]["title"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment