Skip to content

Instantly share code, notes, and snippets.

@daz
Created May 30, 2010 10:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daz/418930 to your computer and use it in GitHub Desktop.
Save daz/418930 to your computer and use it in GitHub Desktop.
Formula 1 results scraper using Mechanize
# Gets race and quali info from f1.com and returns an array table with Position, Driver, Team, Time/Retired, Laps
#
# Eg
# [
# ["1", "Jenson Button", "McLaren-Mercedes", "1:34:09.565", "58"],
# ["2", "Sebastian Vettel", "Red Bull Racing-Renault", "+2.1 secs", "58"],
# ["3", "Lewis Hamilton", "McLaren-Mercedes", "+4.0 secs", "58"],
# ...
require 'rubygems'
require 'mechanize'
year = 2012
venue = 'Australia'
qualifying = false
results = []
a = Mechanize.new
begin
def get_td_text(row, indexes)
if indexes.is_a? Array
indexes.flatten.map { |i| get_td_text(row, i) }
else
row.search("td:nth-child(#{indexes})").text.strip
end
end
a.get("http://www.formula1.com/results/season/#{year}/") do |page|
link = page.link_with(:text => venue)
page = a.click(link)
if qualifying
qualifying_link = page.link_with(:text => 'QUALIFYING')
page = a.click(qualifying_link)
end
page.search('.raceResults tr:not(:first)').each_with_index do |row, i|
indexes = [1, 3, 4]
if qualifying
indexes << case i
when 0..9 then 7
when 10..16 then 6
else 5
end
indexes << 8
else
indexes << [6, 5]
end
row = get_td_text(row, indexes)
results << row unless row[0] == ''
end
end
rescue => e
puts e
end
# Pretty print
require 'terminal-table'
title = [venue, year]
title << 'Qualifying' if qualifying
table = Terminal::Table.new :headings => %w[Pos Driver Team Time Laps], :rows => results
table.align_column 0, :right
table.align_column 3, :right
table.align_column 4, :right
puts title * ' '
puts table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment