Skip to content

Instantly share code, notes, and snippets.

@no-reply
Created August 8, 2012 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save no-reply/3299038 to your computer and use it in GitHub Desktop.
Save no-reply/3299038 to your computer and use it in GitHub Desktop.
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