用rjson

时间:2017-12-11 01:03:44

标签: r web-scraping rjson

我花了很长时间使用R来试图刮取NBA数据,到目前为止我通过反复试验做了一点,但最后我找到了documentation。前段时间我在抓取 shotchartdetail 时遇到了一些问题,当我找到this

时我发现了问题

这可行

为此我就这样做了:

shotURLtotal <- paste0("http://stats.nba.com/stats/shotchartdetail?CFID=33&CFPARAMS=2016-17&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=0&PlusMinus=N&Position=&Rank=N&RookieYear=&Season=2016-17&SeasonSegment=&SeasonType=Regular+Season&TeamID=0&VsConference=&VsDivision=&mode=Advanced&showDetails=0&showShots=1&showZones=0&PlayerPosition=")

Season <- rjson::fromJSON(file = shotURLtotal, method="C")
Names <- Season$resultSets[[1]][[2]]

Season <- data.frame(matrix(unlist(Season$resultSets[[1]][[3]]), ncol = length(Names), byrow = TRUE))

colnames(Season) <- Names

但这不是

但是当我尝试对 shotchartlineupdetail 做同样的事情时,它不起作用,我怀疑它与 CFID 有关,我没有&#39不知道这意味着什么,这就是我的尝试。

shoturl <- "http://stats.nba.com/stats/shotchartlineupdetail/?leagueId=00&season=2016-17&seasonType=Regular+Season&teamId=0&outcome=&location=&month=0&seasonSegment=&dateFrom=&dateTo=&opponentTeamId=0&vsConference=&vsDivision=&gameSegment=&period=0&lastNGames=0&gameId=&group_id=0&contextFilter=&contextMeasure=FGA"


Season <- rjson::fromJSON(file = shoturl, method="C")
Names <- Season$resultSets[[1]][[2]]

Season <- data.frame(matrix(unlist(Season$resultSets[[1]][[3]]), ncol = length(Names), byrow = TRUE))

colnames(Season) <- Names

预期结果

预期结果应该是包含以下列的数据框:

c("GRID_TYPE", "GAME_ID", "GAME_EVENT_ID", "GROUP_ID", "GROUP_NAME", "PLAYER_ID", "PLAYER_NAME", "TEAM_ID", "TEAM_NAME", "PERIOD", "MINUTES_REMAINING", "SECONDS_REMAINING", "EVENT_TYPE", "ACTION_TYPE", "SHOT_TYPE", "SHOT_ZONE_BASIC", "SHOT_ZONE_AREA", "SHOT_ZONE_RANGE", "SHOT_DISTANCE", "LOC_X", "LOC_Y", "SHOT_ATTEMPTED_FLAG", "SHOT_MADE_FLAG", "GAME_DATE", "HTM", "VTM")

你可以这样做:

shoturl <- "http://stats.nba.com/stats/shotchartlineupdetail/?leagueId=00&season=2016-17&seasonType=Regular+Season&teamId=0&outcome=&location=&month=0&seasonSegment=&dateFrom=&dateTo=&opponentTeamId=0&vsConference=&vsDivision=&gameSegment=&period=0&lastNGames=0&gameId=&group_id=0&contextFilter=&contextMeasure=FGA"


Season <- rjson::fromJSON(file = shoturl, method="C")
Names <- Season$resultSets[[1]][[2]]

所以Names将是数据帧的列,问题是不使用 CFID 你得到那些列的数据应该是空的列表,答案是@be_green给出联盟平均值,我需要球队特定数据

1 个答案:

答案 0 :(得分:0)

因此,我认为这里的问题是您需要将PlayerIDTeamID传递给API。以下面的PlayerID = 2544TeamID = 1610612739为例,看来可行:

library(tidyverse)
res <- jsonlite::read_json("https://stats.nba.com/stats/shotchartdetail?AheadBehind=&ClutchTime=&ContextFilter=&ContextMeasure=PTS&DateFrom=&DateTo=&EndPeriod=&EndRange=&GameID=&GameSegment=&LastNGames=0&LeagueID=00&Location=&Month=0&OpponentTeamID=0&Outcome=&Period=0&PlayerID=2544&PlayerPosition=&PointDiff=&Position=&RangeType=&RookieYear=&Season=&SeasonSegment=&SeasonType=Regular+Season&StartPeriod=&StartRange=&TeamID=1610612739&VsConference=&VsDivision=")
# res %>% str(max.level = 3)

header_names <- flatten_chr(res$resultSets[[1]]$headers)
header_names
#>  [1] "GRID_TYPE"           "GAME_ID"             "GAME_EVENT_ID"      
#>  [4] "PLAYER_ID"           "PLAYER_NAME"         "TEAM_ID"            
#>  [7] "TEAM_NAME"           "PERIOD"              "MINUTES_REMAINING"  
#> [10] "SECONDS_REMAINING"   "EVENT_TYPE"          "ACTION_TYPE"        
#> [13] "SHOT_TYPE"           "SHOT_ZONE_BASIC"     "SHOT_ZONE_AREA"     
#> [16] "SHOT_ZONE_RANGE"     "SHOT_DISTANCE"       "LOC_X"              
#> [19] "LOC_Y"               "SHOT_ATTEMPTED_FLAG" "SHOT_MADE_FLAG"     
#> [22] "GAME_DATE"           "HTM"                 "VTM"

res$resultSets[[1]]$rowSet %>%
  map(`[`, 1:24) %>%
  map(~ set_names(., header_names)) %>%
  bind_rows()
#> # A tibble: 8,369 x 24
#>    GRID_TYPE GAME_ID GAME_EVENT_ID PLAYER_ID PLAYER_NAME TEAM_ID TEAM_NAME
#>    <chr>     <chr>           <int>     <int> <chr>         <int> <chr>    
#>  1 Shot Cha~ 002030~            20      2544 LeBron Jam~  1.61e9 Clevelan~
#>  2 Shot Cha~ 002030~            28      2544 LeBron Jam~  1.61e9 Clevelan~
#>  3 Shot Cha~ 002030~            35      2544 LeBron Jam~  1.61e9 Clevelan~
#>  4 Shot Cha~ 002030~            54      2544 LeBron Jam~  1.61e9 Clevelan~
#>  5 Shot Cha~ 002030~            67      2544 LeBron Jam~  1.61e9 Clevelan~
#>  6 Shot Cha~ 002030~            76      2544 LeBron Jam~  1.61e9 Clevelan~
#>  7 Shot Cha~ 002030~           224      2544 LeBron Jam~  1.61e9 Clevelan~
#>  8 Shot Cha~ 002030~           233      2544 LeBron Jam~  1.61e9 Clevelan~
#>  9 Shot Cha~ 002030~           235      2544 LeBron Jam~  1.61e9 Clevelan~
#> 10 Shot Cha~ 002030~           322      2544 LeBron Jam~  1.61e9 Clevelan~
#> # ... with 8,359 more rows, and 17 more variables: PERIOD <int>,
#> #   MINUTES_REMAINING <int>, SECONDS_REMAINING <int>, EVENT_TYPE <chr>,
#> #   ACTION_TYPE <chr>, SHOT_TYPE <chr>, SHOT_ZONE_BASIC <chr>,
#> #   SHOT_ZONE_AREA <chr>, SHOT_ZONE_RANGE <chr>, SHOT_DISTANCE <int>,
#> #   LOC_X <int>, LOC_Y <int>, SHOT_ATTEMPTED_FLAG <int>,
#> #   SHOT_MADE_FLAG <int>, GAME_DATE <chr>, HTM <chr>, VTM <chr>

reprex package(v0.2.1)于2019-03-26创建