I'd like to scrape a table of NBA team stats with rvest, I've tried using:
the table element
library(rvest) url_nba <- "http://stats.nba.com/teams/advanced/#!?sort=TEAM_NAME&dir=-1" team_stats <- url_nba %>% read_html %>% html_nodes('table') %>% html_table
the xpath (via google chrome inspect)
team_stats <- url_nba %>% read_html %>% html_nodes(xpath="/html/body/main/div[2]/div/div[2]/div/div/nba-stat-table/div[1]/div[1]/table") %>% html_table
the css selector (via mozilla inspect):
team_stats <- url_nba %>% read_html %>% html_nodes(".nba-stat-table__overflow > table:nth-child(1)") %>% html_table
but with no luck. Any help would be greatly appreciated.
This question is very similar to this one: How to select a particular section of JSON Data in R?
The data you are requesting is not stored in the html code, thus the failures using rvest. The requested data is stored as a XHR file which and can be accessed directly:
Once the data is loaded into a the nba variable, using httr and jsonlite to clean-up the data:
I highly recommend reading the answer to the question which I linked above.