Created
May 30, 2010 10:01
-
-
Save daz/418930 to your computer and use it in GitHub Desktop.
Formula 1 results scraper using Mechanize
This file contains hidden or 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
# 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