# Write a program in several steps to graph the pairs
#   (rushing yardage, receiving yardage)
# across the 32 NFL teams.
# 
# 1. Start by reading a single team's statistics page.
# 2. Start again by getting a vector of links to team websites
#    (e.g. "https://www.packers.com/").

rm(list=ls())

TEAMS = "https://www.nfl.com/teams"
## (With browser, view page source (HTML) for TEAMS page, search for
## "packers", note "data-link_url" that distinguishes the second occurrence.)

lines = readLines(TEAMS) # many lines of HTML
team.line = grep(pattern='data-link_url="http.*://www', x=lines, value=TRUE)
print(nchar(team.line)) # Very long! We will split it to reveal the team URLs.
team.lines.list = strsplit(x=team.line, split='CTA View Full Site\" data-link_url=\"')
team.lines = team.lines.list[[1]]
team.lines = team.lines[-1] # exclude first; 32 left, each of the form:
# https://www.packers.com/" class="d3-o-me...
for (i in 1:32) { # This loop is for debugging only, to see what's going on:
    cat(sep="", "i=", i,
        ", nchar()=", nchar(team.lines[i]),
        ", line starts: ", substr(x=team.lines[i], start=1, stop=40), "\n")
}

team.links = sub(pattern='(https://www[a-z0-9.]+)/".*', replacement="\\1", x=team.lines)
print(team.links)
