Cannot fetch data from URL NBA stats

喜你入骨 提交于 2020-01-24 15:36:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!