-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipcode.py
62 lines (40 loc) · 1.46 KB
/
zipcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import urllib
import urllib2
from bs4 import BeautifulSoup
from operator import itemgetter
import sys
def zipinfo(zipcode):
url = "http://zipwho.com/?zip=" + zipcode + "&city=&filters=--_--_--_--&state=&mode=zip"
request = urllib2.Request(url, headers={'User-Agent':"Magic Browser"})
result = urllib2.urlopen(request)
page = BeautifulSoup(result)
x = page.find("script").get_text().split("\"")[1].replace('\\n',',').split(',')
namenum = 0
statnum = len(x)/2
data = {}
while(statnum < len(x)):
if x[statnum].isdigit():
x[statnum] = int(x[statnum])
else:
try:
x[statnum] = float(x[statnum])
except ValueError:
pass
data[x[namenum]] = x[statnum]
namenum += 1
statnum += 1
url2 = "http://www.city-data.com/zips/"+zipcode+".html"
request2 = urllib2.Request(url2, headers={'User-Agent':"Magic Browser"})
result2 = urllib2.urlopen(request2)
page2 = BeautifulSoup(result2)
price = '';
for br in page2.find_all("br"):
try:
if br.find("b").get_text() == "Estimated median house or condo value in 2011: ":
price = "$" + br.get_text().split("\n")[0].split("$")[1].split('\r')[0]
except:
pass
data['Median house or condo value'] = price
return data
if __name__ == "__main__":
print(zipinfo(sys.argv[1]))