Created
April 8, 2015 01:45
-
-
Save bycoffe/8674e01a953bd55140d2 to your computer and use it in GitHub Desktop.
Scraper for Chicago runoff election results
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
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