使用 NBA API 获取数据的问题:JSONDecodeError:期望值:第 1 行第 1 列字符 0

问题描述

我运行以下代码以从 NBA API 获取数据,但出现上述错误。任何帮助表示赞赏。

from nba_api.stats.endpoints import leaguedashteamstats
import requests
import json
import pandas as pd
response = leaguedashteamstats.LeagueDashTeamStats(
    team_id=0,game_ids= 0,league_id=0,season= '2020-21',season_type_all_star='Regular Season'

解决方法

这个包好像已经有大约 10 个月没有更新了。您需要 a) 包含 headers 参数(我相信这是 api 需要的 "Referer" 参数,否则它只是“挂起”并且您会超时。

但是 b) 您还需要使查询参数与端点需要的匹配。具体来说,因为 League_id 需要为 '00',因此 0 将不起作用。

我个人更喜欢直接访问源代码而不是使用这个包装器,但如果使用了正确的参数,它仍然可以工作。

from nba_api.stats.endpoints import leaguedashteamstats
import requests
import json
import pandas as pd

response = leaguedashteamstats.LeagueDashTeamStats(
    team_id_nullable='0',league_id_nullable='00',season= '2020-21',season_type_all_star='Regular Season',headers={'Accept': 'application/json,text/plain,*/*','Accept-Encoding': 'gzip,deflate,br','Accept-Language': 'en-US,en;q=0.9','Connection': 'keep-alive','Host': 'stats.nba.com','Origin': 'https://www.nba.com','Referer': 'https://www.nba.com/','sec-ch-ua': '"Google Chrome";v="87","\"Not;A\\Brand";v="99","Chromium";v="87"','sec-ch-ua-mobile': '?1','Sec-Fetch-Dest': 'empty','Sec-Fetch-Mode': 'cors','Sec-Fetch-Site': 'same-site','User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36','x-nba-stats-origin': 'stats','x-nba-stats-token': 'true'})


df = response.get_data_frames()[0]

输出:

print(df.to_string())
       TEAM_ID               TEAM_NAME  GP   W   L  W_PCT   MIN   FGM   FGA  FG_PCT  FG3M  FG3A  FG3_PCT   FTM   FTA  FT_PCT  OREB  DREB   REB   AST   TOV  STL  BLK  BLKA    PF   PFD    PTS  PLUS_MINUS  GP_RANK  W_RANK  L_RANK  W_PCT_RANK  MIN_RANK  FGM_RANK  FGA_RANK  FG_PCT_RANK  FG3M_RANK  FG3A_RANK  FG3_PCT_RANK  FTM_RANK  FTA_RANK  FT_PCT_RANK  OREB_RANK  DREB_RANK  REB_RANK  AST_RANK  TOV_RANK  STL_RANK  BLK_RANK  BLKA_RANK  PF_RANK  PFD_RANK  PTS_RANK  PLUS_MINUS_RANK  CFID                CFPARAMS
0   1610612737           Atlanta Hawks  72  41  31  0.569  48.3  40.8  87.2   0.468  12.4  33.4    0.373  19.7  24.2   0.812  10.6  35.1  45.6  24.1  13.2  7.0  4.8   5.0  19.3  19.8  113.7         2.3        1      11      11          11        11        19        23           17         17         19            12         2         4            5          7         10         5        19         9        24        17         17       18         9        13                9    10           Atlanta Hawks
1   1610612738          Boston Celtics  72  36  36  0.500  48.3  41.5  88.9   0.466  13.6  36.4    0.374  16.1  20.8   0.775  10.6  33.6  44.3  23.5  14.1  7.7  5.3   4.6  20.4  19.3  112.6         1.5        1      16      16          16        16        13        11           19         11         10            10        25        25           16          4         22        15        25        16        13         6         11       24        13        16               13    10          Boston Celtics
2   1610612751           Brooklyn Nets  72  48  24  0.667  48.3  43.1  87.3   0.494  14.2  36.1    0.392  18.1  22.5   0.804   8.9  35.5  44.4  26.8  13.5  6.7  5.3   4.6  19.0  18.9  118.6         4.5        1       4       4           4        11         6        21            1          7         12             2         5         8            6         27          5        14         7        13        27         7          9       13        18         2                7    10           Brooklyn Nets
3   1610612766       Charlotte Hornets  72  33  39  0.458  48.2  39.9  87.8   0.455  13.7  37.0    0.369  15.9  20.9   0.761  10.6  33.2  43.8  26.8  14.8  7.8  4.8   4.8  18.0  18.6  109.5        -1.9        1      19      19          19        19        22        20           22         10          9            14        26        24           21          6         25        19         5        24        10        16         14        5        21        23               23    10       Charlotte Hornets
4   1610612741           Chicago Bulls  72  31  41  0.431  48.3  42.2  88.6   0.476  12.6  34.0    0.370  13.8  17.5   0.791   9.6  35.3  45.0  26.8  15.1  6.7  4.2   5.1  18.9  17.7  110.7        -0.9        1      21      21          21        16        10        14            9         16         17            13        30        30           11         19          8        11         8        27        28        27         18       10        30        21               20    10           Chicago Bulls
5   1610612739     Cleveland Cavaliers  72  22  50  0.306  48.4  38.6  85.8   0.450  10.0  29.7    0.336  16.7  22.4   0.743  10.4  32.3  42.8  23.8  15.5  7.8  4.5   5.9  18.2  20.2  103.8        -8.4        1      26      26          26         6        29        28           25         29         28            30        14         9           26         10         28        24        21        29        12        20         29        7         6        30               28    10     Cleveland Cavaliers
6   1610612742        Dallas Mavericks  72  42  30  0.583  48.1  41.1  87.3   0.470  13.8  38.1    0.362  16.5  21.2   0.778   9.1  34.2  43.3  22.9  12.1  6.3  4.3   3.7  19.4  20.1  112.4         2.3        1       8       8           8        24        18        22           13          8          6            18        18        20           15         25         17        21        26         3        30        26          2       19         7        17               11    10        Dallas Mavericks
7   1610612743          Denver Nuggets  72  47  25  0.653  48.6  43.3  89.2   0.485  12.9  34.2    0.377  15.7  19.5   0.803  10.5  33.9  44.4  26.8  13.5  8.1  4.5   4.5  19.1  19.2  115.1         4.9        1       5       5           5         1         4         9            4         15         16             8        27        27            7          8         19        13         5        12         8        21          7       14        15         8                6    10          Denver Nuggets
8   1610612765         Detroit Pistons  72  20  52  0.278  48.4  38.7  85.6   0.452  11.6  32.9    0.351  17.8  23.4   0.759   9.6  33.1  42.7  24.2  14.9  7.4  5.2   5.8  20.5  20.4  106.6        -4.5        1      29      29          29         6        28        29           24         22         21            22         7         5           24         18         26        25        18        25        17         8         28       26         5        27               25    10         Detroit Pistons
9   1610612744   Golden State Warriors  72  39  33  0.542  48.1  41.3  88.2   0.468  14.6  38.7    0.376  16.6  21.1   0.785   8.0  35.1  43.0  27.7  15.0  8.2  4.8   4.3  21.2  19.5  113.7         1.1        1      14      14          14        24        15        17           14          3          5             9        16        21           13         30         12        22         1        26         6        17          6       28        11        12               14    10   Golden State Warriors
10  1610612745         Houston Rockets  72  17  55  0.236  48.1  39.3  88.5   0.444  13.8  40.6    0.339  16.5  22.3   0.740   9.3  33.3  42.6  23.6  14.7  7.6  5.0   5.3  19.5  19.3  108.8        -7.9        1      30      30          30        24        25        15           28          9          3            28        17        10           27         23         24        27        24        23        14        14         21       21        14        24               27    10         Houston Rockets
11  1610612754          Indiana Pacers  72  34  38  0.472  48.5  43.3  91.2   0.474  12.3  34.0    0.364  16.4  20.7   0.792   9.0  33.7  42.7  27.4  13.5  8.5  6.4   5.3  20.2  18.1  115.3         0.0        1      17      17          17         4         3         3           11         18         18            17        20        26            9         26         21        26         2        13         5         1         23       22        26         6               17    10          Indiana Pacers
12  1610612746             LA Clippers  72  47  25  0.653  48.0  41.8  86.7   0.482  14.3  34.7    0.411  16.2  19.3   0.839   9.4  34.7  44.2  24.4  13.2  7.1  4.1   4.2  19.2  18.1  114.0         6.2        1       5       5           5        30        12        25            5          6         14             1        24        28            1         21         14        17        17         7        20        29          5       17        27        10                2    10             LA Clippers
13  1610612747      Los Angeles Lakers  72  42  30  0.583  48.5  40.6  86.1   0.472  11.1  31.2    0.354  17.2  23.3   0.739   9.7  34.6  44.2  24.7  15.2  7.8  5.4   4.5  19.1  21.3  109.5         2.8        1       8       8           8         4        21        27           12         25         24            21        11         6           28         17         15        16        15        28        11         5          8       16         3        22                8    10      Los Angeles Lakers
14  1610612763       Memphis Grizzlies  72  38  34  0.528  48.3  42.8  91.8   0.467  11.2  31.4    0.356  16.4  21.3   0.771  11.2  35.3  46.5  26.9  13.3  9.1  5.1   5.2  18.7  18.3  113.3         1.0        1      15      15          15        11         7         2           18         24         23            20        19        17           18          2          9         4         4        10         1        12         20        9        25        15               15    10       Memphis Grizzlies
15  1610612748              Miami Heat  72  40  32  0.556  48.3  39.2  83.7   0.468  12.9  36.2    0.358  16.7  21.1   0.790   8.0  33.5  41.5  26.3  14.1  7.9  4.0   4.0  18.9  19.6  108.1         0.0        1      13      13          13        16        26        30           15         14         11            19        13        21           12         29         23        29         9        17         9        30          4       12        10        25               16    10              Miami Heat
16  1610612749         Milwaukee Bucks  72  46  26  0.639  48.1  44.7  91.8   0.487  14.4  37.1    0.389  16.2  21.4   0.760  10.3  37.8  48.1  25.5  13.8  8.1  4.6   4.8  17.3  18.3  120.1         5.9        1       7       7           7        22         1         1            3          5          8             5        23        16           23         12          1         2        14        15         7        19         15        2        24         1                3    10         Milwaukee Bucks
17  1610612750  Minnesota Timberwolves  72  23  49  0.319  48.3  40.7  90.9   0.448  13.1  37.6    0.349  17.6  23.1   0.761  10.5  33.0  43.5  25.6  14.3  8.8  5.5   5.5  20.9  19.9  112.1        -5.6        1      25      25          25        11        20         6           27         12          7            25         8         7           22          9         27        20        11        19         3         3         26       27         8        18               26    10  Minnesota Timberwolves
18  1610612740    New Orleans Pelicans  72  31  41  0.431  48.4  42.5  89.1   0.477  10.6  30.4    0.348  19.0  26.1   0.729  11.7  35.7  47.4  26.0  14.6  7.6  4.4   5.9  18.0  21.3  114.6        -0.3        1      21      21          21         6         9        10            7         27         25            26         4         2           29          1          3         3        10        22        15        24         30        4         2         9               18    10    New Orleans Pelicans
19  1610612752         New York Knicks  72  41  31  0.569  48.4  39.4  86.5   0.456  11.8  30.0    0.392  16.4  20.9   0.784   9.7  35.5  45.1  21.4  12.9  7.0  5.1   5.4  20.5  17.9  107.0         2.3        1      11      11          11         6        24        26           21         21         27             3        21        23           14         16          7         9        29         6        21        11         25       25        29        26               10    10         New York Knicks
20  1610612760   Oklahoma City Thunder  72  22  50  0.306  48.2  38.8  88.0   0.441  11.9  35.1    0.339  15.5  21.3   0.725   9.9  35.7  45.6  22.1  16.1  7.0  4.4   5.3  18.1  18.6  105.0       -10.6        1      26      26          26        19        27        19           29         20         13            29        29        17           30         14          3         6        27        30        23        23         21        6        23        28               30    10   Oklahoma City Thunder
21  1610612753           Orlando Magic  72  21  51  0.292  48.1  38.3  89.2   0.429  10.9  31.8    0.343  16.6  21.4   0.775  10.4  35.1  45.4  21.8  12.8  6.9  4.4   5.3  17.2  18.6  104.0        -9.3        1      28      28          28        22        30         8           30         26         22            27        15        15           17         11         10         7        28         5        25        22         24        1        20        29               29    10           Orlando Magic
22  1610612755      Philadelphia 76ers  72  49  23  0.681  48.4  41.4  86.9   0.476  11.3  30.1    0.374  19.6  25.5   0.767  10.0  35.0  45.1  23.7  14.4  9.1  6.2   4.7  20.2  21.0  113.6         5.6        1       3       3           3         6        14        24            8         23         26            11         3         3           20         13         13        10        22        21         1         2         12       23         4        14                5    10      Philadelphia 76ers
23  1610612756            Phoenix Suns  72  51  21  0.708  48.6  43.3  88.3   0.490  13.1  34.6    0.378  15.6  18.7   0.834   8.8  34.2  42.9  26.9  12.5  7.2  4.3   3.6  19.1  18.0  115.3         5.8        1       2       2           2         1         2        16            2         13         15             7        28        29            2         28         18        23         3         4        19        25          1       14        28         7                4    10            Phoenix Suns
24  1610612757  Portland Trail Blazers  72  42  30  0.583  48.1  41.3  91.1   0.453  15.7  40.8    0.385  17.8  21.6   0.823  10.6  33.9  44.5  21.3  11.1  6.9  5.0   4.6  18.9  19.1  116.1         1.8        1       8       8           8        24        15         4           23          2          2             6         6        13            3          3         20        12        30         1        25        13         10       10        16         5               12    10  Portland Trail Blazers
25  1610612758        Sacramento Kings  72  31  41  0.431  48.1  42.6  88.6   0.481  12.1  33.3    0.364  16.4  22.0   0.745   9.4  32.0  41.4  25.5  13.4  7.5  5.0   4.7  19.4  18.7  113.7        -3.7        1      21      21          21        24         8        13            6         19         20            16        21        11           25         22         30        30        12        11        16        15         13       20        19        11               24    10        Sacramento Kings
26  1610612759       San Antonio Spurs  72  33  39  0.458  48.6  41.9  90.5   0.462   9.9  28.4    0.350  17.4  22.0   0.792   9.3  34.6  43.9  24.4  11.4  7.0  5.1   5.1  18.0  18.6  111.1        -1.7        1      19      19          19         1        11         7           20         30         30            24         9        12           10         24         16        18        16         2        22        10         19        3        22        20               21    10       San Antonio Spurs
27  1610612761         Toronto Raptors  72  27  45  0.375  48.1  39.7  88.7   0.448  14.5  39.3    0.368  17.4  21.3   0.815   9.4  32.1  41.6  24.1  13.2  8.6  5.4   5.6  21.2  19.5  111.3        -0.5        1      24      24          24        24        23        12           26          4          4            15        10        17            4         20         29        28        20         8         4         4         27       28        12        19               19    10         Toronto Raptors
28  1610612762               Utah Jazz  72  52  20  0.722  48.2  41.3  88.1   0.468  16.7  43.0    0.389  17.2  21.5   0.799  10.6  37.6  48.3  23.7  14.2  6.6  5.2   3.9  18.5  19.0  116.4         9.3        1       1       1           1        19        17        18           16          1          1             4        12        14            8          4          2         1        23        18        29         8          3        8        17         4                1    10               Utah Jazz
29  1610612764      Washington Wizards  72  34  38  0.472  48.3  43.2  90.9   0.475  10.2  29.0    0.351  20.1  26.2   0.769   9.7  35.5  45.2  25.5  14.4  7.3  4.1   4.8  21.6  22.0  116.6        -1.8        1      17      17          17        11         5         5           10         28         29            23         1         1           19         15          6         8        13        20        18        28         15       30         1         3               22    10      Washington Wizards