Skip to content

Instantly share code, notes, and snippets.

@jimhester
Created August 19, 2013 18:13
Show Gist options
  • Save jimhester/6272263 to your computer and use it in GitHub Desktop.
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.
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