Last active
September 25, 2015 01:58
-
-
Save drwelby/844447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Requires a free InfoChimps account to get census data | |
You'll need to enter your InfoChimps API key in the perc_black function | |
''' | |
from BeautifulSoup import BeautifulSoup | |
import urllib2 | |
from geopy import geocoders | |
import simplejson | |
G = geocoders.Google() | |
def geocode(address): | |
'''Geocodes an address to a lat and lon''' | |
try: | |
place, (lat,lon) = G.geocode(address) | |
return lat,lon | |
except: | |
return 0,0 | |
def perc_black(lat,lon): | |
'''Returns the percentage of the population that black. | |
If no census data is found, returns -1.''' | |
API_KEY = 'YOUR INFOCHIMPS API KEY' | |
URL = 'http://api.infochimps.com/web/an/census/combined.json?apikey=' + API_KEY | |
URL = URL + "&lat=%s&long=%s" % (lat,lon) | |
try: | |
result = simplejson.load(urllib2.urlopen(URL)) | |
return float(result['percent_black']) | |
except: | |
return -1.0 | |
states = ['AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA', | |
'HI','ID','IL','IN','IA','KS','KY','LA','ME','MD', | |
'MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ', | |
'NM','NY','NC','ND','OH','OK','OR','PA','RI','SC', | |
'SD','TN','TX','UT','VT','VA','WA','WV','WI','WY',] | |
total = 0 | |
geocoded = 0 | |
census = 0 | |
black = 0 | |
for state in states: | |
url = 'http://www.plannedparenthood.org/health-center/findCenter.asp?s=%s&p=0&o=1&c=0' % (state) | |
html = urllib2.urlopen(url) | |
soup = BeautifulSoup(html) | |
divs = soup.findAll('div', {'class':'center vcard'}) | |
for div in divs: | |
total += 1 | |
address = div.findAll('span', {'class':'center_google_address'})[0].text | |
lat,lon = geocode(address) | |
if lat > 0: | |
geocoded += 1 | |
p = perc_black(lat,lon) | |
if p > 0: | |
census += 1 | |
if p > 0.5: | |
black += 1 | |
print "Total Addresses: %s" % (total) | |
print "Geocoded Addresses: %d" % (geocoded) | |
print "Addresses with census info: %s" % (census) | |
print "Addresses that are >50%% black: %s" % (black) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment