title: Scraping a Dynamic Webpage in R
body: <p>I am having difficulty scraping a dynamically rendered page. I have tried similar posts here for answers, but I know so little about javascript that I cannot understand them.</p>
  
  <p>I want to scrape each table here: <a href="https://www.espn.com/golf/leaderboard/_/tournamentId/401056558" rel="nofollow noreferrer">https://www.espn.com/golf/leaderboard/_/tournamentId/401056558</a></p>
  
  <p>I have completed the one for leaderboard, but I can't figure out how to get the tables for Player Stats and Course Stats since they're dynamically rendered.</p>
  
  <p>I am too ignorant of javascript to know where to begin. I've read V8 is a useful package, but I cannot figure out why.</p>
  
  <pre><code># clears the R workspace
  rm(list = ls())
  
  # sets the working directory to the directory to the current working directory, which is where the
  # output files will be printed
  setwd(getwd())
  
  # loads in xml2 for the read_html function
  library(xml2)
  # loads in rvest for the html_text function
  library(rvest)
  # for handling the dynamically rendered javascript
  library(V8)
  
  url &lt;- 'https://www.espn.com/golf/leaderboard/_/tournamentId/401056558'
  golf_webpage &lt;- read_html(url)
  
  
  # this block of code loads in and formats the leaderboard
  
  # loads in the leaderboard data
  leaderboard_text_html &lt;- html_nodes(golf_webpage, '.Table2__td')
  leaderboard_text &lt;- html_text(leaderboard_text_html)
  
  #creates a matrix with 10 rows for each of the 10 leaderboard columns and makes the number of columns the number of golfers
  leaderboard &lt;- matrix(leaderboard_text, nrow =10 , ncol = length(leaderboard_text)/10)
  
  # transposes the matrix so each row is a golfer and each column is a leaderboard column
  leaderboard &lt;- t(leaderboard)
  </code></pre>
  
  <p>I want to figure out how to switch to the player stats and course stats tables to read them in.</p>
  
  <p>Edit: I tried reading in all the the tables into a list of tables. It says there are 3 tables, which is the number I want, but only the last one (the leaderboard) is readable.</p>
  
  <pre><code># loads the xml2 library
  library(xml2)
  
  # loads in the espn golf webpage as html
  golf_webpage &lt;- 
  read_html('https://www.espn.com/golf/leaderboard/_/tournamentId/401056558')
  
  tables_list &lt;- golf_webpage %&gt;%
    html_nodes("table") %&gt;%
    html_table(fill = TRUE)
  </code></pre>