Skip to content

Instantly share code, notes, and snippets.

@bycoffe
Created April 8, 2015 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bycoffe/8674e01a953bd55140d2 to your computer and use it in GitHub Desktop.
Save bycoffe/8674e01a953bd55140d2 to your computer and use it in GitHub Desktop.
Scraper for Chicago runoff election results
require 'json'
require 'nokogiri'
require 'open-uri'
results = []
open("http://www.chicagoelections.com/ap/results.htm") do |req|
doc = Nokogiri::HTML(req.read)
race = {:updated => doc.css("#ResultsContainer")[0].text.sub(/Last Updated: /, '')}
doc.css("table")[2..-1].each_slice(2) do |tables|
race[:contest] = tables[0].css("#contest").text
meta = tables[0].css("#processed").text.match(/^(?<reporting>\d+) out of (?<total>\d+) precincts \((?<pct>[.\d]+) %\)/)
race[:precincts_reporting] = meta['reporting'].to_i
race[:total_precincts] = meta['total'].to_i
race[:pct_reporting] = meta['pct'].to_f
race[:candidates] = tables[1].css("tr").map do |row|
{:name => row.css("#cand").text,
:votes => row.css("#votes").text.gsub(/,/, '').to_i,
:pct => row.css("#pct").text.gsub(/ %/, '').to_f}
end
results << race
end
puts JSON.pretty_generate(results)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment