Skip to content

Instantly share code, notes, and snippets.

@no-reply
Created Aug 8, 2012
Embed
What would you like to do?
get county from melissadata
#! /usr/bin/python2
''' Takes an address as args and prints the county from MelissaData.com
Usage:
./county.py 1234 Fake Street, Sometown, CA 12345
Works with quotes, too:
./county.py "1234 Fake Street, Sometown, CA 12345"
'''
import sys
import re
import urllib
import urllib2
address = ''
for arg in sys.argv:
if not '.py' in arg:
address += arg
address += ' '
queryurl = "http://www.melissadata.com/lookups/AddressCheck.asp?" + urllib.urlencode({"InData": address})
html = urllib2.urlopen(queryurl).read()
if "Address Verified" in html:
cindex = html.index('County')
print re.search('<b>(.*)</b>', html[cindex:cindex + 40]).group(1).capitalize()
else:
raise Exception("Melissa returned some kind of error.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment