You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言出现Cannot Open Connection错误:爬取NBA API投篮数据遇阻

Fixing "Cannot Open Connection" Error When Scraping NBA.com API in R

Hey Daniel, I’ve dealt with this exact frustration when pulling shot chart data from the NBA Stats API in R—let’s walk through the most common fixes for that "Cannot Open Connection" error.

Key Issues & Solutions

1. Missing HTTPS Prefix in Your URL

First off, your URL starts with stats.nba.com instead of https://stats.nba.com. NBA’s API requires HTTPS, so omitting this will immediately fail to establish a connection. Always make sure your full URL begins with https://.

2. NBA API Blocks Default R Request Headers

The NBA Stats API uses anti-scraping measures that block requests with R’s default user agent. You need to mimic a browser’s request headers to get access.

Here’s a working example using the httr and jsonlite packages (install them first if you haven’t):

# Install required packages if needed
if (!require(httr)) install.packages("httr")
if (!require(jsonlite)) install.packages("jsonlite")

library(httr)
library(jsonlite)

# Full, corrected URL (fill in the truncated parameters at the end)
url <- "https://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+Season&TeamID=0&VsConference=&VsDivision="

# Set browser-like headers
headers <- add_headers(
  `User-Agent` = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
  `Referer` = "https://www.nba.com/",
  `Accept-Language` = "en-US,en;q=0.9"
)

# Send the request and parse the response
response <- GET(url, headers)
if (http_status(response)$category == "Success") {
  shot_data <- fromJSON(content(response, "text"))
  # Extract the relevant data frame (usually the first result set)
  shot_chart <- shot_data$resultSets[[1]]$rowSet
  colnames(shot_chart) <- shot_data$resultSets[[1]]$headers
  print(head(shot_chart))
} else {
  cat("Request failed with status:", http_status(response)$message)
}

3. Ensure Truncated URL Parameters Are Complete

Your original URL was cut off at the end—make sure all required parameters are included (like Season=2017-18 and SeasonType=Regular+Season). Missing critical parameters can also cause connection or parsing errors.

4. Check for Rate Limiting

If you’re making multiple requests quickly, the NBA API might temporarily block your IP. Add small delays between requests using Sys.sleep(2) if you’re scraping multiple players/seasons.


内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:42:29