问题
I'm having an issue fetching JSON data from the stats.nba.com endpoints. For example, this url http://stats.nba.com/stats/teamgamelog?LeagueID=00&Season=2016-17&SeasonType=Regular+Season&teamid=1610612761 works fine in my browser, but when I try to read it into R, I receive errors.
jsonlite::fromJSON
, RJSONIO::fromJSON
, RCurl::getURL
, HTTR::get
all hang forever until I kill them.
rjson::fromJSON
gives me an error "unexpected character 'h'"
I'm not sure if there is something denying programmatic access, and how I would get around that.
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.3
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
`
回答1:
Try downloading the file first before trying to process it:
library(curl)
library(jsonlite)
curl_download("http://stats.nba.com/stats/teamgamelog?LeagueID=00&Season=2016-17&SeasonType=Regular+Season&teamid=1610612761", "nba.json")
jsonlist<-fromJSON( "nba.json")
df<-as.data.frame(jsonlist$resultSets$rowSet)
names(df)<-jsonlist$resultSets$headers[[1]]
parameters<-jsonlist$parameters
来源:https://stackoverflow.com/questions/43690554/cannot-fetch-data-from-url-nba-stats