Friday, May 23, 2008
I have a small fantasy football related project that I'm going to be working on that requires access to the full 2008 NFL Schedule.  I decided to use Hpricot, and scrape the schedule from ESPN.  The CSV file I created with the output from the below script can be found here: nfl-schedule.csv (11.48 KB)  

Download script (parse_schedule.rb)

#!ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'

class Game
   attr_accessor :date, :week, :away_team, :home_team, :time

   def to_s
      "#{@date} #{@time} #{@away_team} at #{@home_team}"
   end

   def to_csv
      "#{@week},#{@date.gsub(",", "")},#{@time},#{@away_team},#{@home_team}"
   end
end

def parse_games(doc)
   games = []
   doc.search("//table[@class='tablehead']//tr").each do |tr|
      @week = tr.search("/td/a").inner_html if(tr[:class] == 'stathead')
      @date = tr.at("td").inner_html if(tr[:class] == 'colhead')

      teams = []
      tr.at("td").search("a").each do |team|
         teams << team.inner_html
      end

      if(teams.size == 2)
         @time = tr.search("td:eq(1)").inner_html
         game = Game.new()
         game.date = @date
         game.week = @week
         game.time = @time
         game.away_team = teams[0]
         game.home_team = teams[1]
         games << game
      end
   end
   games
end

games = parse_games(Hpricot(open("http://sports.espn.go.com/nfl/schedule")))
games.each do |g|
   puts g.to_csv
end
puts "Total games: #{games.size}"
Saturday, May 24, 2008 12:35:58 AM (Eastern Daylight Time, UTC-04:00) | Comments [12] | #
Tracked by:
"2009 NFL Schedule in CSV Format" (Steve Eichert) [Trackback]
Saturday, June 28, 2008 5:22:16 PM (Eastern Daylight Time, UTC-04:00)
Hello,

I was wondering how do I run your code? Cant seem to find the complier.

Also do you have the code for the project you were working on as well.

Sergio
Sergio
Saturday, June 28, 2008 5:42:19 PM (Eastern Daylight Time, UTC-04:00)
It's a ruby script so you'll need to install ruby to run the script. Once ruby is installed you need to install hpricot (http://code.whytheluckystiff.net/hpricot/wiki/InstallingHpricot). Once you have ruby and hpricot installed and have the script above downloaded you can run it by running the following form a command prompt.

> ruby parse_schedule.rb
Saturday, June 28, 2008 9:40:22 PM (Eastern Daylight Time, UTC-04:00)
Thanks for the info.

What language did you code your fantasy football project in. I have a simular task was wondering if you mind sharing your code.

Sergio
Sergio
Saturday, June 28, 2008 9:48:05 PM (Eastern Daylight Time, UTC-04:00)
Thanks for the info.

What language did you code your fantasy football project in. I have a simular task was wondering if you mind sharing your code.

Sergio
Sergio
Wednesday, August 06, 2008 6:22:43 PM (Eastern Daylight Time, UTC-04:00)
Just wanted to thank you for the csv schedule! You've saved me tons of time on my own project.
Wednesday, August 06, 2008 6:26:57 PM (Eastern Daylight Time, UTC-04:00)
Jaime, Glad it was useful! Enjoy

~ Steve
Monday, August 11, 2008 2:10:04 PM (Eastern Daylight Time, UTC-04:00)
Seems a lot of people are using hpricot for doing scrapes of data from HTML these days. Thanks for doing this, it is exactly what I needed, and the hpricot stuff will help should I need some more info too.

Thanks again!
Wednesday, October 08, 2008 3:48:28 PM (Eastern Daylight Time, UTC-04:00)
I'm using your great example, but how can I save this information into a table instead of creating a file?

Thanks!
Becca
Friday, May 01, 2009 4:55:34 PM (Eastern Daylight Time, UTC-04:00)
Any chance of getting a 2009 version? :)
Chris
Saturday, May 30, 2009 1:54:02 PM (Eastern Daylight Time, UTC-04:00)
good topic
Monday, November 16, 2009 6:48:48 AM (Eastern Standard Time, UTC-05:00)
"Any chance of getting a 2009 version? :)"
I think you may have the chance :(
Sunday, November 22, 2009 7:08:18 PM (Eastern Standard Time, UTC-05:00)
Steve, You have an awesome script. Is there a way to get it to output like the following. I have a football pool and the data input is as follows below. Your help would be greatly apreciated.

The Columns are as follows /count/week/date/time(24hr)/Visitor abrv/null/Home abrv/null/0

1,1,9/10/09,20:30:00,TEN,,PIT,,0
2,1,9/13/09,13:00:00,MIA,,ATL,,0
3,1,9/13/09,13:00:00,DEN,,CIN,,0
4,1,9/13/09,13:00:00,MIN,,CLE,,0
5,1,9/13/09,13:00:00,JAC,,IND,,0
6,1,9/13/09,13:00:00,DET,,NO,,0
7,1,9/13/09,13:00:00,DAL,,TB,,0
8,1,9/13/09,13:00:00,PHI,,CAR,,0
9,1,9/13/09,13:00:00,KC,,BAL,,0
10,1,9/13/09,13:00:00,NYJ,,HOU,,0
11,1,9/13/09,16:15:00,WAS,,NYG,,0
12,1,9/13/09,16:15:00,SF,,ARI,,0
13,1,9/13/09,16:15:00,STL,,SEA,,0
14,1,9/13/09,20:20:00,CHI,,GB,,0
15,1,9/14/09,19:00:00,BUF,,NE,,0
16,1,9/14/09,22:15:00,SD,,OAK,,0
17,2,9/20/09,13:00:00,CAR,,ATL,,0
18,2,9/20/09,13:00:00,MIN,,DET,,0
19,2,9/20/09,13:00:00,CIN,,GB,,0
20,2,9/20/09,13:00:00,HOU,,TEN,,0
21,2,9/20/09,13:00:00,OAK,,KC,,0
22,2,9/20/09,13:00:00,NE,,NYJ,,0
23,2,9/20/09,13:00:00,NO,,PHI,,0
24,2,9/20/09,13:00:00,STL,,WAS,,0
25,2,9/20/09,13:00:00,ARI,,JAC,,0
26,2,9/20/09,16:05:00,TB,,BUF,,0
27,2,9/20/09,16:05:00,SEA,,SF,,0
28,2,9/20/09,16:15:00,PIT,,CHI,,0
29,2,9/20/09,16:15:00,BAL,,SD,,0
30,2,9/20/09,16:15:00,CLE,,DEN,,0
31,2,9/20/09,20:20:00,NYG,,DAL,,0
32,2,9/21/09,20:30:00,IND,,MIA,,0
33,3,9/27/09,13:00:00,WAS,,DET,,0
34,3,9/27/09,13:00:00,GB,,STL,,0
35,3,9/27/09,13:00:00,SF,,MIN,,0
36,3,9/27/09,13:00:00,ATL,,NE,,0
37,3,9/27/09,13:00:00,TEN,,NYJ,,0
38,3,9/27/09,13:00:00,KC,,PHI,,0
39,3,9/27/09,13:00:00,NYG,,TB,,0
40,3,9/27/09,13:00:00,CLE,,BAL,,0
41,3,9/27/09,13:00:00,JAC,,HOU,,0
42,3,9/27/09,16:05:00,NO,,BUF,,0
43,3,9/27/09,16:05:00,CHI,,SEA,,0
44,3,9/27/09,16:15:00,MIA,,SD,,0
45,3,9/27/09,16:15:00,PIT,,CIN,,0
46,3,9/27/09,16:15:00,DEN,,OAK,,0
47,3,9/27/09,20:20:00,IND,,ARI,,0
48,3,9/28/09,20:30:00,CAR,,DAL,,0
49,4,10/4/09,13:00:00,DET,,CHI,,0
50,4,10/4/09,13:00:00,CIN,,CLE,,0
51,4,10/4/09,13:00:00,SEA,,IND,,0
52,4,10/4/09,13:00:00,NYG,,KC,,0
53,4,10/4/09,13:00:00,BAL,,NE,,0
54,4,10/4/09,13:00:00,TB,,WAS,,0
55,4,10/4/09,13:00:00,TEN,,JAC,,0
56,4,10/4/09,13:00:00,OAK,,HOU,,0
57,4,10/4/09,16:05:00,NYJ,,NO,,0
58,4,10/4/09,16:05:00,BUF,,MIA,,0
59,4,10/4/09,16:15:00,STL,,SF,,0
60,4,10/4/09,16:15:00,DAL,,DEN,,0
61,4,10/4/09,20:20:00,SD,,PIT,,0
62,4,10/5/09,20:30:00,GB,,MIN,,0
63,5,10/11/09,13:00:00,CLE,,BUF,,0
64,5,10/11/09,13:00:00,DAL,,KC,,0
65,5,10/11/09,13:00:00,MIN,,STL,,0
66,5,10/11/09,13:00:00,OAK,,NYG,,0
67,5,10/11/09,13:00:00,TB,,PHI,,0
68,5,10/11/09,13:00:00,PIT,,DET,,0
69,5,10/11/09,13:00:00,WAS,,CAR,,0
70,5,10/11/09,13:00:00,CIN,,BAL,,0
71,5,10/11/09,16:05:00,ATL,,SF,,0
72,5,10/11/09,16:15:00,JAC,,SEA,,0
73,5,10/11/09,16:15:00,HOU,,ARI,,0
74,5,10/11/09,16:15:00,NE,,DEN,,0
75,5,10/11/09,20:20:00,IND,,TEN,,0
76,5,10/12/09,20:30:00,NYJ,,MIA,,0
77,6,10/18/09,13:00:00,HOU,,CIN,,0
78,6,10/18/09,13:00:00,DET,,GB,,0
79,6,10/18/09,13:00:00,BAL,,MIN,,0
80,6,10/18/09,13:00:00,NYG,,NO,,0
81,6,10/18/09,13:00:00,CLE,,PIT,,0
82,6,10/18/09,13:00:00,CAR,,TB,,0
83,6,10/18/09,13:00:00,KC,,WAS,,0
84,6,10/18/09,13:00:00,STL,,JAC,,0
85,6,10/18/09,16:05:00,ARI,,SEA,,0
86,6,10/18/09,16:05:00,PHI,,OAK,,0
87,6,10/18/09,16:15:00,TEN,,NE,,0
88,6,10/18/09,16:15:00,BUF,,NYJ,,0
89,6,10/18/09,20:20:00,CHI,,ATL,,0
90,6,10/19/09,20:30:00,DEN,,SD,,0
91,7,10/25/09,13:00:00,CHI,,CIN,,0
92,7,10/25/09,13:00:00,GB,,CLE,,0
93,7,10/25/09,13:00:00,SD,,KC,,0
94,7,10/25/09,13:00:00,IND,,STL,,0
95,7,10/25/09,13:00:00,SF,,HOU,,0
96,7,10/25/09,13:00:00,MIN,,PIT,,0
97,7,10/25/09,13:00:00,NE,,TB,,0
98,7,10/25/09,16:05:00,BUF,,CAR,,0
99,7,10/25/09,16:05:00,NYJ,,OAK,,0
100,7,10/25/09,16:15:00,NO,,MIA,,0
101,7,10/25/09,16:15:00,ATL,,DAL,,0
102,7,10/25/09,20:20:00,ARI,,NYG,,0
103,7,10/26/09,20:30:00,PHI,,WAS,,0
104,8,11/1/09,13:00:00,HOU,,BUF,,0
105,8,11/1/09,13:00:00,CLE,,CHI,,0
106,8,11/1/09,13:00:00,SEA,,DAL,,0
107,8,11/1/09,13:00:00,STL,,DET,,0
108,8,11/1/09,13:00:00,MIN,,GB,,0
109,8,11/1/09,13:00:00,SF,,IND,,0
110,8,11/1/09,13:00:00,MIA,,NYJ,,0
111,8,11/1/09,13:00:00,DEN,,BAL,,0
112,8,11/1/09,16:05:00,OAK,,SD,,0
113,8,11/1/09,16:05:00,JAC,,TEN,,0
114,8,11/1/09,16:15:00,NYG,,PHI,,0
115,8,11/1/09,16:15:00,CAR,,ARI,,0
116,8,11/2/09,20:30:00,ATL,,NO,,0
117,9,11/8/09,13:00:00,WAS,,ATL,,0
118,9,11/8/09,13:00:00,ARI,,CHI,,0
119,9,11/8/09,13:00:00,BAL,,CIN,,0
120,9,11/8/09,13:00:00,HOU,,IND,,0
121,9,11/8/09,13:00:00,MIA,,NE,,0
122,9,11/8/09,13:00:00,GB,,TB,,0
123,9,11/8/09,13:00:00,KC,,JAC,,0
124,9,11/8/09,16:05:00,DET,,SEA,,0
125,9,11/8/09,16:05:00,CAR,,NO,,0
126,9,11/8/09,16:15:00,SD,,NYG,,0
127,9,11/8/09,16:15:00,TEN,,SF,,0
128,9,11/8/09,20:20:00,DAL,,PHI,,0
129,9,11/9/09,20:30:00,PIT,,DEN,,0
130,10,11/12/09,20:20:00,CHI,,SF,,0
131,10,11/15/09,13:00:00,BUF,,TEN,,0
132,10,11/15/09,13:00:00,NO,,STL,,0
133,10,11/15/09,13:00:00,TB,,MIA,,0
134,10,11/15/09,13:00:00,DET,,MIN,,0
135,10,11/15/09,13:00:00,JAC,,NYJ,,0
136,10,11/15/09,13:00:00,CIN,,PIT,,0
137,10,11/15/09,13:00:00,DEN,,WAS,,0
138,10,11/15/09,13:00:00,ATL,,CAR,,0
139,10,11/15/09,16:05:00,KC,,OAK,,0
140,10,11/15/09,16:15:00,DAL,,GB,,0
141,10,11/15/09,16:15:00,SEA,,ARI,,0
142,10,11/15/09,16:15:00,PHI,,SD,,0
143,10,11/15/09,20:20:00,NE,,IND,,0
144,10,11/16/09,20:30:00,BAL,,CLE,,0
145,10,11/16/09,20:30:00,NYG,,HOU,,0
146,11,11/19/09,20:20:00,MIA,,CAR,,0
147,11,11/22/09,13:00:00,WAS,,DAL,,0
148,11,11/22/09,13:00:00,CLE,,DET,,0
149,11,11/22/09,13:00:00,SF,,GB,,0
150,11,11/22/09,13:00:00,PIT,,KC,,0
151,11,11/22/09,13:00:00,ATL,,NYG,,0
152,11,11/22/09,13:00:00,NO,,TB,,0
153,11,11/22/09,13:00:00,BUF,,JAC,,0
154,11,11/22/09,13:00:00,IND,,BAL,,0
155,11,11/22/09,13:00:00,SEA,,MIN,,0
156,11,11/22/09,16:05:00,ARI,,STL,,0
157,11,11/22/09,16:15:00,NYJ,,NE,,0
158,11,11/22/09,16:15:00,CIN,,OAK,,0
159,11,11/22/09,16:15:00,SD,,DEN,,0
160,11,11/22/09,20:20:00,PHI,,CHI,,0
161,11,11/23/09,20:30:00,TEN,,HOU,,0
162,12,11/26/09,12:30:00,GB,,DET,,0
163,12,11/26/09,16:15:00,OAK,,DAL,,0
164,12,11/26/09,20:20:00,NYG,,DEN,,0
165,12,11/29/09,13:00:00,TB,,ATL,,0
166,12,11/29/09,13:00:00,MIA,,BUF,,0
167,12,11/29/09,13:00:00,CLE,,CIN,,0
168,12,11/29/09,13:00:00,ARI,,TEN,,0
169,12,11/29/09,13:00:00,SEA,,STL,,0
170,12,11/29/09,13:00:00,CHI,,MIN,,0
171,12,11/29/09,13:00:00,CAR,,NYJ,,0
172,12,11/29/09,13:00:00,WAS,,PHI,,0
173,12,11/29/09,13:00:00,IND,,HOU,,0
174,12,11/29/09,16:05:00,KC,,SD,,0
175,12,11/29/09,16:05:00,JAC,,SF,,0
176,12,11/29/09,20:20:00,PIT,,BAL,,0
177,12,11/30/09,20:30:00,NE,,NO,,0
178,13,12/3/09,20:20:00,NYJ,,BUF,,0
179,13,12/6/09,13:00:00,PHI,,ATL,,0
180,13,12/6/09,13:00:00,STL,,CHI,,0
181,13,12/6/09,13:00:00,DET,,CIN,,0
182,13,12/6/09,13:00:00,TEN,,IND,,0
183,13,12/6/09,13:00:00,DEN,,KC,,0
184,13,12/6/09,13:00:00,NO,,WAS,,0
185,13,12/6/09,13:00:00,TB,,CAR,,0
186,13,12/6/09,13:00:00,HOU,,JAC,,0
187,13,12/6/09,13:00:00,OAK,,PIT,,0
188,13,12/6/09,16:05:00,SD,,CLE,,0
189,13,12/6/09,16:15:00,SF,,SEA,,0
190,13,12/6/09,16:15:00,DAL,,NYG,,0
191,13,12/6/09,16:15:00,MIN,,ARI,,0
192,13,12/6/09,20:20:00,NE,,MIA,,0
193,13,12/7/09,20:30:00,BAL,,GB,,0
194,14,12/10/09,20:20:00,PIT,,CLE,,0
195,14,12/13/09,13:00:00,NO,,ATL,,0
196,14,12/13/09,13:00:00,GB,,CHI,,0
197,14,12/13/09,13:00:00,STL,,TEN,,0
198,14,12/13/09,13:00:00,DEN,,IND,,0
199,14,12/13/09,13:00:00,BUF,,KC,,0
200,14,12/13/09,13:00:00,NYJ,,TB,,0
201,14,12/13/09,13:00:00,MIA,,JAC,,0
202,14,12/13/09,13:00:00,DET,,BAL,,0
203,14,12/13/09,13:00:00,SEA,,HOU,,0
204,14,12/13/09,13:00:00,CIN,,MIN,,0
205,14,12/13/09,13:00:00,CAR,,NE,,0
206,14,12/13/09,16:05:00,WAS,,OAK,,0
207,14,12/13/09,16:15:00,SD,,DAL,,0
208,14,12/13/09,20:20:00,PHI,,NYG,,0
209,14,12/14/09,20:30:00,ARI,,SF,,0
210,15,12/17/09,20:20:00,IND,,JAC,,0
211,15,12/19/09,20:20:00,DAL,,NO,,0
212,15,12/20/09,13:00:00,NE,,BUF,,0
213,15,12/20/09,13:00:00,ARI,,DET,,0
214,15,12/20/09,13:00:00,MIA,,TEN,,0
215,15,12/20/09,13:00:00,CLE,,KC,,0
216,15,12/20/09,13:00:00,HOU,,STL,,0
217,15,12/20/09,13:00:00,ATL,,NYJ,,0
218,15,12/20/09,13:00:00,SF,,PHI,,0
219,15,12/20/09,13:00:00,GB,,PIT,,0
220,15,12/20/09,13:00:00,CHI,,BAL,,0
221,15,12/20/09,16:05:00,CIN,,SD,,0
222,15,12/20/09,16:05:00,OAK,,DEN,,0
223,15,12/20/09,16:15:00,TB,,SEA,,0
224,15,12/20/09,20:20:00,MIN,,CAR,,0
225,15,12/21/09,20:30:00,NYG,,WAS,,0
226,16,12/25/09,19:30:00,SD,,TEN,,0
227,16,12/27/09,13:00:00,BUF,,ATL,,0
228,16,12/27/09,13:00:00,KC,,CIN,,0
229,16,12/27/09,13:00:00,OAK,,CLE,,0
230,16,12/27/09,13:00:00,SEA,,GB,,0
231,16,12/27/09,13:00:00,BAL,,PIT,,0
232,16,12/27/09,13:00:00,HOU,,MIA,,0
233,16,12/27/09,13:00:00,JAC,,NE,,0
234,16,12/27/09,13:00:00,TB,,NO,,0
235,16,12/27/09,13:00:00,CAR,,NYG,,0
236,16,12/27/09,13:00:00,DEN,,PHI,,0
237,16,12/27/09,16:05:00,STL,,ARI,,0
238,16,12/27/09,16:05:00,DET,,SF,,0
239,16,12/27/09,16:15:00,NYJ,,IND,,0
240,16,12/27/09,20:20:00,DAL,,WAS,,0
241,16,12/28/09,20:30:00,MIN,,CHI,,0
242,17,1/3/10,13:00:00,IND,,BUF,,0
243,17,1/3/10,13:00:00,JAC,,CLE,,0
244,17,1/3/10,13:00:00,PHI,,DAL,,0
245,17,1/3/10,13:00:00,CHI,,DET,,0
246,17,1/3/10,13:00:00,ATL,,TB,,0
247,17,1/3/10,13:00:00,NO,,CAR,,0
248,17,1/3/10,13:00:00,NE,,HOU,,0
249,17,1/3/10,13:00:00,SF,,STL,,0
250,17,1/3/10,13:00:00,PIT,,MIA,,0
251,17,1/3/10,13:00:00,NYG,,MIN,,0
252,17,1/3/10,13:00:00,CIN,,NYJ,,0
253,17,1/3/10,16:15:00,GB,,ARI,,0
254,17,1/3/10,16:15:00,WAS,,SD,,0
255,17,1/3/10,16:15:00,TEN,,SEA,,0
256,17,1/3/10,16:15:00,BAL,,OAK,,0
257,17,1/3/10,16:15:00,KC,,DEN,,0
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Search
Archive
Links
Categories
Admin Login
Sign In
Blogroll
Themes
Pick a theme: