Skip to content

Instantly share code, notes, and snippets.

@dannypage
Created December 30, 2013 19:30
Show Gist options
  • Save dannypage/8186815 to your computer and use it in GitHub Desktop.
Save dannypage/8186815 to your computer and use it in GitHub Desktop.
Scraping the Pregame betting site for analysis of their results. As they use Divs for their tables, it took some trial and error to find the right fields with Nokogiri. This will create a CSV and covers the 2013 year, although it craps out quickly. Hmm. No records are shown, where I'm pretty sure there were before. Hacked up in a few hours, so t…
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'net/http'
require 'cgi'
require 'fileutils'
require "date"
begin
date = '20131229'
output = File.open("pregame_results_2013.csv","w")
output << 'date,pro,sport,pick,score,odds,size,wl,result\n'
while date != '20121231'
puts "Working on #{date}."
doc = Nokogiri::HTML(open("http://pregame.com/pregamepros/picks/date.aspx?date=#{date}"))
a = doc.xpath('//*[@id="contmain"]/div/div[3]')
start = 3
ending = a.children.count
while start < ending
b = a.xpath("./div[#{start}]")
result_date = Date.strptime(date,'%Y%m%d')
result_date = '"' + result_date.strftime('%m/%d/%Y') + '"'
pro = '"' + b.xpath('./div[1]').text.gsub(/(\r\n(\s)+)/,'').sub(/\u00a0/,'').strip + '"'
sport = '"' + b.xpath('./div[2]').text.gsub(/(\r\n(\s)+)/,'').sub(/\u00a0/,'').strip + '"'
pick = '"' + b.xpath('./div[3]').text.gsub(/(\r\n(\s)+)/,'').sub(/\u00a0/,'').strip + '"'
score = '"' + b.xpath('./div[4]').text.gsub(/(\r\n(\s)+)/,'').sub(/\u00a0/,'').strip + '"'
odds = '"' + b.xpath('./div[5]').text.gsub(/(\r\n(\s)+)/,'').sub(/\u00a0/,'').strip + '"'
size = '"' + b.xpath('./div[6]').text.gsub(/(\r\n(\s)+)/,'').sub(/\u00a0/,'').strip + '"'
wl = '"' + b.xpath('./div[7]').text.gsub(/(\r\n(\s)+)/,'').sub(/\u00a0/,'').strip + '"'
result = '"' + b.xpath('./div[8]').text.gsub(/(\r\n(\s)+)/,'').sub(/\u00a0/,'').strip + '"'
start += 1
if pro != ''
output << [result_date,pro,sport,pick,score,odds,size,wl,result].join(',')
output << "\n"
end
end
d = Date.strptime(date,'%Y%m%d')
d -= 1
date = d.strftime('%Y%m%d')
end
output.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment