Screen Scraping College Football Statistics

As a follow-up to my post of the correlation of Academic Ranking and Football Rankings in the Big Ten, I thought I would look that the relationship between two different kinds of Football Rankings: the recruiting ranking assigned by Rivals and the actual results on the field. To that end, I went to collect the data programmically because I am doing a time-series analysis and I didn’t want to do data-entry.

My first stop was to find a free service that exposes this data on the web. No luck – either the data was a service that cost money or the data was presented as a web page. Since I have never screen-scraped using F# (and I am cheap), I chose option #2.

My first data point was the recruiting ranking found here. When I inspected the source of the page, I caught a break – the data is actually stored as Json on the page.

So firing up Visual Studio, I created a solution with 1 F# project and 2 C# projects:

I then wrote a unit test to check that something is being returned:

[TestMethod]

publicvoid getRecrutRankings_RetunsExpected()

{

var rankings = RankingProvider.getRecrutRankings("2012");

Assert.AreNotEqual(0, rankings.Length);

}

I then went over the F#. I created the RankingProvider type and then add a function that pulls in the rankings for a given year:

Lines 2 through 12 are language-agnostic. You would write the exact same code in C#/VB.NET with a slightly different syntax.

Line 13 is where things get interesting. I used the :?> operator to cast the Json to a typed structure. :?> wins as the weirdest symbol I have ever used in computer programming. I guess I haven’t been programming long enough?

Lines 14 and 15 is where you can see why F# is better than C#. I created a function that takes the Json and pushes it into a tuple. With no iteration, the code is both easier to read and less likely to have bugs

Hoping to press my luck, I went over the the other page (the one that holds the standings from the actual games) to see if they used Json. No dice – so back to mid-2000s screen scraping. I created a function that loads the table into an XML document and then searches for a given school.

Lines 2-7 are identical to the prior function so they should be combined into a single function that can be independently testable.

Lines 8-13 are language-agnostic. You would write the exact same code in C#/VB.NET with a slightly different syntax.

Lines 14-18 is where F# really shines. Like the prior function, by using functional programming techniques in F#, I saved myself time, avoid bugs, and made the code much more intuitive.

I am making a web call for each function call– this should be optimized so the call is made once and the xmlDocument is passed in. This would also make the function much more testable (even without a mocking framework)

Next up, I needed to call this function for each of the Big Ten Schools:

This is purely F# and is a pure joy to write (and look the least amount of time). Note that the sort is on the second element of the tuple and that the list is reversed because the second element is the wins-losses so F# is sorting ascending on the number of wins. Since Seq does not have a rev function, I turned it into a List, which does have the rev function

Some might ask “Why didn’t you use type-providers?” My answer is “I tried, but I couldn’t get them to work.” For example, here is the code that I used for the type provider when parsing the xmlDocument:

xmlDocument.LoadXml(data);

let document = XmlProvider<xmlDocument>

The problem is that the type provider expects a uri (and I can’t find an overload to pass in the document). It looks like type providers are more designed for providers that are ready to, well, provide (Web Services, Databases, etc..) versus jerry-rigged data (like screen scraping).

In any event, with these two functions, ready, I went to the UI project and decided to see how the teams did in 2012 on the field compared to how the teams did in recruiting 2 years before:

staticvoid Main(string[] args)

{

Console.WriteLine("Start");

Console.WriteLine("——-Rankings");

var rankings = RankingProvider.getRecrutRankings("2010");

foreach (var school in rankings)

{

Console.WriteLine(school.Item1 + ":" + school.Item2);

}

Console.WriteLine("——-Standings");

var standings = RankingProvider.getConferenceStandings("2012");

foreach (var school in standings)

{

Console.WriteLine(school.Item1 + ":" + school.Item2);

}

Console.WriteLine("End");

Console.ReadKey();

}

And the results:

I have no idea if a 2-year lag between recruiting and rankings is the right number – perhaps an analysis of the correct lag will be done. After all, between red-shirt freshmen, transfer rules, and attrition, there are plenty of variables the determine when a recruiting class has the biggest impact. Also, the standings are a blend of recruiting classes and since I am not evaluating individual players, I can’t go to that level of detail. 2 years out seems reasonable, but as Bluto famiously once said

staticmember getBlutoQuote() =

"Seven years of college down the drain.";

:

the average might be different. In any event, I now have the data I want so the next step is to analyze it to see if there is any correlation. At first glance, there might be something – the top 4 schools for recruiting all finished in the top 4 in the standings – but the bottom 4 is more muddled with only Illinois doing poorly in both recruiting and the standings.

More to come…

Advertisements

Like this:

LikeLoading...

Related

2 Responses to Screen Scraping College Football Statistics

Hi, the XmlProvider accepts a xml document instead of a url, but you have to pass it as a string by using the Parse method instead of the Load method. In any case, it doesn’t work very well with html. But you can easily use the JsonProvider for the other part. See here: https://gist.github.com/ovatsus/8123945