网页抓取 NBA 统计数据 nba_api 端点

发布于 2025-01-15 13:51:13 字数 214 浏览 2 评论 0原文

我正在尝试从 NBA 统计数据中抓取数据,特别是 团队的得分。我正在寻找此页面的 nba_api 端点,以便我可以抓取数据。

如何找到终点?

I am trying to scrape the data from NBA stats, specifically the team's boxscore. I am looking for the nba_api endpoint for this page so that i can scrape the data.

How can I find the endpoint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗梦空间 2025-01-22 13:51:13

您可以通过打开开发工具 (sfht-ctrl-i) 并在网络 -> 下查看来找到端点。 XHR(您可能需要刷新页面)。观察面板是否开始弹出请求,并找到包含您的数据的请求。转到标头查找发出请求所需的信息:

import requests
import pandas as pd


url = 'https://stats.nba.com/stats/leaguegamelog'
headers= {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
          'Referer': 'https://www.nba.com/'}
payload = {
    'Counter': '1000',
    'DateFrom': '',
    'DateTo': '',
    'Direction': 'DESC',
    'LeagueID': '00',
    'PlayerOrTeam': 'T',
    'Season': '2021-22',
    'SeasonType': 'Regular Season',
    'Sorter': 'DATE'}

jsonData = requests.get(url, headers=headers, params=payload).json()


rows = jsonData['resultSets'][0]['rowSet']
columns = jsonData['resultSets'][0]['headers']

df = pd.DataFrame(rows, columns=columns)

输出:

print(df)
     SEASON_ID     TEAM_ID TEAM_ABBREVIATION  ...  PTS PLUS_MINUS VIDEO_AVAILABLE
0        22021  1610612759               SAS  ...  110          2               1
1        22021  1610612744               GSW  ...  108         -2               1
2        22021  1610612761               TOR  ...   93          5               1
3        22021  1610612755               PHI  ...   88         -5               1
4        22021  1610612738               BOS  ...  124         20               1
       ...         ...               ...  ...  ...        ...             ...
2133     22021  1610612754               IND  ...  122         -1               1
2134     22021  1610612749               MIL  ...  127         23               1
2135     22021  1610612751               BKN  ...  104        -23               1
2136     22021  1610612744               GSW  ...  121          7               1
2137     22021  1610612747               LAL  ...  114         -7               1

[2138 rows x 29 columns]

You find the endpoint by opening Dev Tools (sfht-ctrl-i) and look under Network -> XHR (you may need to refresh the page). Watch the panel for the requests to start popping up, and find the one that has your data. Go to Headers to find the info needed to make the request:

import requests
import pandas as pd


url = 'https://stats.nba.com/stats/leaguegamelog'
headers= {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
          'Referer': 'https://www.nba.com/'}
payload = {
    'Counter': '1000',
    'DateFrom': '',
    'DateTo': '',
    'Direction': 'DESC',
    'LeagueID': '00',
    'PlayerOrTeam': 'T',
    'Season': '2021-22',
    'SeasonType': 'Regular Season',
    'Sorter': 'DATE'}

jsonData = requests.get(url, headers=headers, params=payload).json()


rows = jsonData['resultSets'][0]['rowSet']
columns = jsonData['resultSets'][0]['headers']

df = pd.DataFrame(rows, columns=columns)

Output:

print(df)
     SEASON_ID     TEAM_ID TEAM_ABBREVIATION  ...  PTS PLUS_MINUS VIDEO_AVAILABLE
0        22021  1610612759               SAS  ...  110          2               1
1        22021  1610612744               GSW  ...  108         -2               1
2        22021  1610612761               TOR  ...   93          5               1
3        22021  1610612755               PHI  ...   88         -5               1
4        22021  1610612738               BOS  ...  124         20               1
       ...         ...               ...  ...  ...        ...             ...
2133     22021  1610612754               IND  ...  122         -1               1
2134     22021  1610612749               MIL  ...  127         23               1
2135     22021  1610612751               BKN  ...  104        -23               1
2136     22021  1610612744               GSW  ...  121          7               1
2137     22021  1610612747               LAL  ...  114         -7               1

[2138 rows x 29 columns]
最佳男配角 2025-01-22 13:51:13

我不是一个超级体育迷,但这似乎是这样的: 免费NBA Boxscore API
您可以尝试将 CSS、JS 和 HTML 段与站点隔离。

它位于 div 的源代码中。

I'm not a huge sports fan, but this seems like it: A free NBA Boxscore API
You could attempt to isolate the CSS, JS and HTML segments from the site.

It is in the source of a div.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文