Skip to content

Instantly share code, notes, and snippets.

@bmander
Created January 12, 2012 21:32
Show Gist options
  • Save bmander/1603263 to your computer and use it in GitHub Desktop.
Save bmander/1603263 to your computer and use it in GitHub Desktop.
scrape car2go san diego site for vehicle locations
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