Created
August 19, 2013 18:13
-
-
Save jimhester/6272263 to your computer and use it in GitHub Desktop.
Get Hex team colors for NBA, NFL, NHL, and MLB from http://teamcolors.arc90.com/ for use in R.
This file contains 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
url = 'http://teamcolors.arc90.com/' | |
library(httr) | |
get_colors = function(league=c('NBA', 'NFL', 'NHL', 'MLB')){ | |
league = match.arg(league) | |
color_data = parse_html(GET('http://teamcolors.arc90.com/')) | |
rbind.fill(xpathApply(color_data, | |
paste('//*[@id="', league, '"]/li[@class="team"]', sep=''), | |
parse_team)) | |
} | |
parse_team = function(node){ | |
name = xmlValue(node[['h3']]) | |
colors = unlist(lapply(getNodeSet(node, './/span'), xmlValue)) | |
names(colors) = paste('color', 1:length(colors), sep='') | |
data.frame(name=name, t(colors)) | |
} | |
parse_html = function(resp){ | |
library(XML) | |
htmlParse(content(resp, type='text')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment