1

I am scraping from the NBA.com API for some shots data. The url that I am using is

url = "stats.nba.com/stats/shotchartdetail?CFID=33&CFPARAMS=2017-18&ContextFilter=&ContextMeasure=FGA&DateFrom=&DateTo=&GameID=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerID=101107&PlusMinus=N&PlayerPosition=&Rank=N&RookieYear=&Season=2017-18&SeasonSegment=&SeasonType=Regular%20Season&TeamID=0&VsConference=&VsDivision="

This website can be easily verified to exist by copying and pasting into your browser. However, when I enter the line

data = rjson::fromJSON(file = url)

I get the error: Error in file(con, "r") : cannot open the connection ... HTTP status was '403 Forbidden'.

I have tried adding http and https to the url but to no avail. Why is R not reading this url that clearly exists?

Daniel
  • 33
  • 9

1 Answers1

1

Overview

You need to download the data into and then import that .json file inside of fromJSON(). I've shown how to go about extracting the two data frames contained in the list object marvin.williams.shot.data:

  1. Marvin William's individual 2017-2018 shot data; and
  2. NBA league average shot data from the 2017-2018 season.

Reproducible Example

# load necessary packages
library( jsonlite )

# load necessary data
download.file( url = "http://stats.nba.com/stats/shotchartdetail?CFID=33&CFPARAMS=2017-18&ContextFilter=&ContextMeasure=FGA&DateFrom=&DateTo=&GameID=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerID=101107&PlusMinus=N&PlayerPosition=&Rank=N&RookieYear=&Season=2017-18&SeasonSegment=&SeasonType=Regular%20Season&TeamID=0&VsConference=&VsDivision="
               , destfile = "stats_nba.json" )

# transfrom into data frame
marvin.williams.shot.data <- 
  fromJSON( txt = "stats_nba.json" )

# view results
lapply( X = marvin.williams.shot.data, FUN = class)
# $resource
# [1] "character"
# 
# $parameters
# [1] "list"
# 
# $resultSets
# [1] "data.frame"

# transfrom the matrix into a data frame
player.shotchart.df <-
  as.data.frame( marvin.williams.shot.data$resultSets$rowSet[[1]]
                 , stringsAsFactors = FALSE )

# assign colnames
colnames( player.shotchart.df ) <-
  marvin.williams.shot.data$resultSets$headers[[1]]

# view results
dim( player.shotchart.df ) # [1] 563  24

# transfrom the matrix into a data frame
league.average.df <-
  as.data.frame( marvin.williams.shot.data$resultSets$rowSet[[2]]
                 , stringsAsFactors = FALSE )

# assign colnames
colnames( league.average.df ) <-
  marvin.williams.shot.data$resultSets$headers[[2]]

# view results
dim( league.average.df ) # [1] 20  7

# end of script #
Cristian E. Nuno
  • 2,822
  • 2
  • 19
  • 33