Skip to content

Instantly share code, notes, and snippets.

Last active Sep 29, 2015
What would you like to do?
Madison Parking Ramp Scrape

Madison Parking Ramp Scrape

A simple django management command to scrape the status of Madison parking garages and save to a database model. It's the basis for the parking garage information at the bottom of this page.

from import BaseCommand
from parking.models import Garage
import requests
import lxml
from lxml import html
import time, datetime
from django.utils.encoding import smart_str, smart_unicode
class Command(BaseCommand):
help = 'Scrapes Madison Parking Garage data'
def handle(self, *args, **options):
self.stdout.write('\nScraping finished at %s\n' % str(
#search URL and assign to variable r
r = requests.get('')
#create variable tree from r's content
tree = lxml.html.fromstring(r.content)
#search the tree for the given element
elements = tree.cssselect("div.dataRow")
#for each element in the variable
for div in elements:
#select inner divs
divs = div.cssselect('div')
#set variables
name = divs[1].text_content().strip()
spots = divs[2].text_content().strip()
time =
obj = Garage.objects.get(name=name)
obj.spots = spots
obj.time = time
except Garage.DoesNotExist:
obj = Garage(name=name, spots=spots, time=time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment