11 May 2011 at 21:11
Terry Moore
Ajax, How To, Programming, Sports
1 Comment
I was at lunch a few weeks ago at a sports bar with several co-workers and started discussing sports with one of our developers. Our discussion led to the question about how the NHL consisted of players from so many different countries compared to other US based sports. When I got back to work I did a couple of Google searches looking to see if someone already had that information for me. But the search didn’t turn up any results (at least not for this year ). But it did lead the the NHL.com website that had all the information about the players but it was displayed in a HTML table that span over 30 webpages. This is where being a programmer pays off.
I decided to write a quick script to parse out this data and place it in an CSV file. I was going to write it in Python but since I was in Windows I decided to to just use my favorite Windows scripting language, AutoIT. Below you will find the complete script that I used to loop through the 3o webpages on NHL.com and grab the contents of the 2010-11 players bio table.
#include <Array.au3>
#include <IE.au3>
#include <File.au3>
$csvFile = "c:\temp\players.csv"
for $i = 1 to 30
$ieObject = _IECreate ("http://www.nhl.com/ice/app?service=page&page=playerstats&fetchKey=20112ALLAASAll&viewName=bios&sort=player.birthCountryAbbrev&pg=" & $i)
$table = _IETableGetCollection ($ieObject, 3)
$aTableData = _IETableWriteToArray ($table, True)
FileOpen($csvFile, 1);
For $r = 1 to UBound($aTableData,1) - 1
For $c = 0 to UBound($aTableData,2) - 1
FileWrite ($csvFile, """" & StringStripWS($aTableData[$r][$c], 3) & """")
if $c = 18 Then
FileWrite ($csvFile, @CRLF)
Else
FileWrite ($csvFile, ",")
EndIf
Next
Next
Next
Note: This doesn’t do any error checking.
I then imported the data to a MySQL database and ran couple of PHP scripts againts the data to get these results.
Here is the number of players in the NHL broken down by birth country.
Country the NHL players were born in.
| AUT: 3 |
BLR: 3 |
BRA: 1 |
| BRN: 1 |
CAN: 482 |
CHE: 3 |
| CZE: 39 |
DEU: 10 |
DNK: 6 |
| FIN: 22 |
FRA: 1 |
ITA: 1 |
| JPN: 1 |
KAZ: 1 |
LTU: 1 |
| LVA: 4 |
NOR: 2 |
POL: 1 |
| RUS: 26 |
SVK: 12 |
SVN: 2 |
| SWE: 54 |
UKR: 4 |
USA: 211 |
Another stat that I though would be interesting was to find out what state the US born NHL players came from. So here it is:
State that US born NHL player where born in.
| AK: 6 |
CA: 8 |
CO: 3 |
| CT: 8 |
DE: 1 |
FL: 1 |
| IL: 10 |
IN: 2 |
MA: 17 |
| MD: 1 |
MI: 34 |
MN: 41 |
| MO: 3 |
NC: 2 |
ND: 3 |
| NE: 1 |
NH: 3 |
NJ: 4 |
| NY: 33 |
OH: 2 |
PA: 14 |
| TX: 1 |
UT: 1 |
WA: 2 |
| WI: 10 |