Archive for January, 2007

Python to grab zip codes.

Friday, January 19th, 2007

I had a need for a list of zip codes and some other relavant information and a need to tinker with Python… So here is a small script that will go to a few zip code sites and save the codes and other info to individual text files for later parsing. I must say this was relatively quick and easy compared to what I would have had to write in C++ to do the same thing.

*NOTE: Don’t use this code to plague the sites, they normally shut you down around 130 hits any way. Although, I programmed a way around that, but due to those interested in DoS attacks, I will not post that here. If you have a need for the zip codes, I can make my zipcode database available to you.

import urllib2

zip=10000
while zip < 99999:
#address = “http://www.zip-codes.com/zip-code/” + str(zip) + “/zip-code-” + str(zip) + “.asp”
address = “http://zipinfo.com/cgi-local/zipsrch.exe?cnty=cnty&ac=ac&tz=tz&ll=ll&msa=msa&zip=” + str(zip) + “&Go=Go”;
website = urllib2.urlopen(address)
website_html = website.read()

print “Processing zipcode: ” + str(zip)

filename=”data/” + str(zip) + “.html”
myfile = open(filename,’w')
myfile.write(website_html)
myfile.close()
zip = zip + 1;