Home > Software engineering >  Scraping data from TeamRankings.com
Scraping data from TeamRankings.com

Time:01-04

I want to scrape some NBA data from TeamRankings.com for my program in python. Here is an example link:

https://www.teamrankings.com/nba/stat/effective-field-goal-pct?date=2023-01-03

I only need the "Last 3" column data. I want to be able to set the date to whatever I want with a constant variable. There are a few other data points I want that are on different links but I will be able to figure that part out if this gets figured out.

I have tried using https://github.com/tymiguel/TeamRankingsWebScraper but it is outdated and did not work for me.

CodePudding user response:

The easiest way will be to use pandas.read_html:

import pandas as pd

url = 'https://www.teamrankings.com/nba/stat/effective-field-goal-pct?date=2023-01-03'

df = pd.read_html(url)[0]
print(df)

Prints:

    Rank          Team   2022 Last 3 Last 1   Home   Away   2021
0      1      Brooklyn  58.8%  64.5%  68.3%  59.4%  58.1%  54.2%
1      2        Denver  57.8%  62.8%  52.2%  59.5%  56.4%  55.5%
2      3        Boston  56.8%  54.6%  51.1%  58.2%  55.1%  54.0%
3      4    Sacramento  56.3%  56.9%  48.4%  59.1%  53.4%  52.5%
4      5  Golden State  56.3%  53.2%  52.5%  56.9%  55.6%  55.4%
5      6        Dallas  56.0%  59.5%  50.0%  55.8%  56.2%  54.0%
6      7      Portland  55.5%  58.6%  65.5%  57.3%  54.3%  51.5%
7      8     Minnesota  55.3%  52.1%  59.2%  55.7%  54.9%  53.8%
8      9          Utah  55.3%  53.9%  53.7%  58.1%  53.0%  55.1%
9     10  Philadelphia  55.3%  57.3%  56.4%  54.5%  56.2%  53.6%
10    11     Cleveland  55.1%  57.7%  60.9%  56.7%  53.1%  53.7%
11    12    Washington  54.6%  61.4%  56.9%  54.7%  54.5%  53.2%
12    13       Chicago  54.6%  57.3%  54.7%  55.7%  53.5%  53.7%
13    14       Indiana  54.5%  60.3%  53.8%  56.1%  52.8%  53.1%
14    15   New Orleans  54.4%  52.5%  56.5%  56.2%  52.5%  51.8%
15    16       Phoenix  54.1%  51.6%  44.8%  54.8%  53.5%  55.0%
16    17   LA Clippers  54.1%  57.8%  52.2%  52.3%  55.8%  53.0%
17    18     LA Lakers  54.0%  56.6%  53.8%  53.7%  54.3%  53.7%
18    19   San Antonio  53.1%  54.6%  47.4%  53.4%  52.8%  52.7%
19    20       Orlando  52.9%  48.0%  44.5%  54.6%  50.9%  50.2%
20    21     Milwaukee  52.8%  45.5%  42.2%  55.0%  50.4%  54.0%
21    22       Memphis  52.8%  54.0%  51.0%  53.8%  51.8%  52.1%
22    23         Miami  52.6%  54.6%  52.9%  53.1%  52.1%  54.0%
23    24      New York  52.2%  51.4%  57.4%  53.9%  50.6%  51.3%
24    25       Atlanta  52.2%  51.5%  53.7%  51.5%  53.0%  54.2%
25    26     Okla City  52.2%  50.9%  44.6%  52.6%  51.7%  49.7%
26    27       Detroit  51.5%  52.3%  45.1%  52.7%  50.5%  49.4%
27    28       Toronto  51.1%  51.3%  52.7%  51.3%  50.8%  51.0%
28    29       Houston  51.0%  50.0%  51.8%  50.2%  51.6%  53.4%
29    30     Charlotte  50.3%  52.0%  51.1%  49.3%  51.2%  54.3%

If you want only Last 3 column:

print(df[['Team', 'Last 3']])

Prints:

            Team Last 3
0       Brooklyn  64.5%
1         Denver  62.8%
2         Boston  54.6%
3     Sacramento  56.9%

...
  • Related