gimmethedata123
gimmethedata123

Reputation: 65

Webscraping Page issue (code no longer works)

I had set up some code to scrape the following page

https://www.nba.com/stats/players/catch-shoot

Below is my code which used to run perfectly fine, but when I tried running it just now I got the following error: "Error in json_data$resultSets[[3]] : subscript out of bounds".

Could someone please let me know what the issue is? Thanks in advance!

library(httr)
library(jsonlite)
library(dplyr)
library(janitor)
library(data.table)
library(magrittr)
library(tidyverse)
library(openxlsx)

# Base URL
base_url <- "https://stats.nba.com/stats/leaguedashptstats"

################### Drives

# Define the query parameters
params <- list(
  "College" = "",
  "Conference" = "",
  "Country" = "",
  "DateFrom" = "",
  "DateTo" = "",
  "Division" = "",
  "DraftPick" = "",
  "DraftYear" = "",
  "GameScope" = "",
  "Height" = "",
  "ISTRound" = "",
  "LastNGames" = "0",
  "LeagueID" = "00",
  "Location" = "",
  "Month" = "0",
  "OpponentTeamID" = "0",
  "Outcome" = "",
  "PORound" = "0",
  "PerMode" = "PerGame",
  "PlayerExperience" = "",
  "PlayerOrTeam" = "Player",
  "PlayerPosition" = "",
  "PtMeasureType" = "Drives",
  "Season" = "2024-25",
  "SeasonSegment" = "",
  "SeasonType" = "Regular Season",
  "StarterBench" = "",
  "TeamID" = "0",
  "VsConference" = "",
  "VsDivision" = "",
  "Weight" = ""
)

# Headers required by the NBA stats API
headers <- c(
  "User-Agent" = paste(
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
    "AppleWebKit/537.36 (KHTML, like Gecko)",
    "Chrome/120.0.0.0 Safari/537.36"
  ),
  "Accept-Language" = "en-US,en;q=0.9",
  "Accept" = "application/json, text/plain, */*",
  "Cache-Control" = "no-cache",
  "Connection" = "keep-alive",
  "Host" = "stats.nba.com",
  "Origin" = "https://www.nba.com",
  "Pragma" = "no-cache",
  "Referer" = "https://www.nba.com/",
  "Sec-Fetch-Dest" = "empty",
  "Sec-Fetch-Mode" = "cors",
  "Sec-Fetch-Site" = "same-site",
  "x-nba-stats-origin" = "stats",
  "x-nba-stats-token" = "true"
)

# Make the GET request
response <- GET(url = base_url, add_headers(.headers = headers), query = params, timeout(10))

# Check if the request was successful
if (response$status_code != 200) {
  print(paste("Request failed with status code", response$status_code))
  print(content(response, as = "text"))
} else {
  # Parse the JSON response
  json_data <- content(response, as = "text", encoding = "UTF-8")
  json_data <- fromJSON(json_data)
  
  # Extract headers and row data
  #result_sets <- json_data$resultSets[[1]]
  #column_headers <- result_sets$headers
  #row_data <- result_sets$rowSet
  
  # Create a DataFrame from the row data and column headers
  df <- as.data.frame(do.call(rbind, json_data$resultSets[[3]]), stringsAsFactors = FALSE)

}

Upvotes: -4

Views: 67

Answers (0)

Related Questions