Created
January 12, 2012 21:32
-
-
Save bmander/1603263 to your computer and use it in GitHub Desktop.
scrape car2go san diego site for vehicle locations
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
import urllib | |
import json | |
URL = "http://www.car2go.com/portal/sandiego/page/mybookings/mapEnlarged.faces" | |
KEY = "ptvMap.data = " | |
def grab_sandiego_vehicles(): | |
#get site HTML | |
fp = urllib.urlopen( URL ) | |
sitestr = fp.read() | |
#grab the line containing the object with vehicle data | |
start = sitestr.index( KEY ) | |
end = sitestr.index( "\n", start ) | |
linestr = sitestr[start:end] | |
# cut the line down to just a JSON string | |
jsonstr = linestr.strip()[len(KEY):-1] | |
#parse JSON | |
data = json.loads( jsonstr ) | |
return data['vehicles'] | |
def tooltip_data_to_dict(td): | |
ret = {} | |
for item in td: | |
ret[item['label']]=item['value'] | |
return ret | |
def vehicle_json_to_csv(vehicles): | |
print "tin,electric,plate,cleanliness,charge,address,lon,lat" | |
for vehicle in vehicles: | |
td = tooltip_data_to_dict(vehicle['tooltip']['data']) | |
print "%s,%s,\"%s\",\"%s\",\"%s\",\"%s\",%s,%s"%(vehicle['vin'], \ | |
vehicle['electric'], \ | |
td['Number plate'], \ | |
td['Cleanliness<br />interior/exterior'], \ | |
td['State of charge'], \ | |
td['Address'], \ | |
vehicle['location']['x'], \ | |
vehicle['location']['y'] ) | |
if __name__=='__main__': | |
vehicles = grab_sandiego_vehicles() | |
vehicle_json_to_csv(vehicles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment