-1
URL = 'https://www.basketball-reference.com/leagues/NBA_2019.html'
content = requests.get(URL)
soup = BeautifulSoup(content.text, 'html.parser')
table = soup.find_all('table', {'class' : 'sortable stats_table now_sortable'})
rows = table.find_all('td')
for i in rows:
    print(i.get_text())

I want to get content of the table with team per game stats from this website but I got error

>>>AttributeError: 'NoneType' object has no attribute 'find_all'
Humayun Ahmad Rajib
  • 1,502
  • 1
  • 10
  • 22
Aleksander Ikleiw
  • 2,549
  • 1
  • 8
  • 26

3 Answers3

0

The table that you want is dynamically loaded, meaning it not loaded into the html when you first make a request to the page. So, the table you are searching for does not yet exist.

To scrape sites that use javascript, you can look into using selenium webdriver and PhantomJS, better described by this post –> https://stackoverflow.com/a/26440563/13275492

YulkyTulky
  • 886
  • 1
  • 6
  • 20
0

Actually you can use pandas.read_html() which will read the all tables in nice format. it's will return tables as list. so you can access it as DataFrame with index such as print(df[0]) for example

import pandas as pd

df = pd.read_html("https://www.basketball-reference.com/leagues/NBA_2019.html")

print(df)

enter image description here

Humayun Ahmad Rajib
  • 1,502
  • 1
  • 10
  • 22
0

The tables (with the exception of a few) in these sports reference sites are within the comments. You would need to pull out the comments, then render these tables with pandas.

import requests
from bs4 import BeautifulSoup, Comment
import pandas as pd

headers = {'User-Agent': 
           'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}

page = "https://www.basketball-reference.com/leagues/NBA_2019.html"
pageTree = requests.get(page, headers=headers)
pageSoup = BeautifulSoup(pageTree.content, 'html.parser')
comments = pageSoup.find_all(string=lambda text: isinstance(text, Comment))


for each in comments:
    if 'table' in each and 'id="team-stats-per_game"' in each:
        df = pd.read_html(each, attrs = {'id': 'team-stats-per_game'})[0]

Output:

print (df)
      Rk                     Team   G     MP    FG  ...  STL  BLK   TOV    PF    PTS
0    1.0         Milwaukee Bucks*  82  241.2  43.4  ...  7.5  5.9  13.9  19.6  118.1
1    2.0   Golden State Warriors*  82  241.5  44.0  ...  7.6  6.4  14.3  21.4  117.7
2    3.0     New Orleans Pelicans  82  240.9  43.7  ...  7.4  5.4  14.8  21.1  115.4
3    4.0      Philadelphia 76ers*  82  241.5  41.5  ...  7.4  5.3  14.9  21.3  115.2
4    5.0    Los Angeles Clippers*  82  241.8  41.3  ...  6.8  4.7  14.5  23.3  115.1
5    6.0  Portland Trail Blazers*  82  242.1  42.3  ...  6.7  5.0  13.8  20.4  114.7
6    7.0   Oklahoma City Thunder*  82  242.1  42.6  ...  9.3  5.2  14.0  22.4  114.5
7    8.0         Toronto Raptors*  82  242.4  42.2  ...  8.3  5.3  14.0  21.0  114.4
8    9.0         Sacramento Kings  82  240.6  43.2  ...  8.3  4.4  13.4  21.4  114.2
9   10.0       Washington Wizards  82  243.0  42.1  ...  8.3  4.6  14.1  20.7  114.0
10  11.0         Houston Rockets*  82  241.8  39.2  ...  8.5  4.9  13.3  22.0  113.9
11  12.0            Atlanta Hawks  82  242.1  41.4  ...  8.2  5.1  17.0  23.6  113.3
12  13.0   Minnesota Timberwolves  82  241.8  41.6  ...  8.3  5.0  13.1  20.3  112.5
13  14.0          Boston Celtics*  82  241.2  42.1  ...  8.6  5.3  12.8  20.4  112.4
14  15.0           Brooklyn Nets*  82  243.7  40.3  ...  6.6  4.1  15.1  21.5  112.2
15  16.0       Los Angeles Lakers  82  241.2  42.6  ...  7.5  5.4  15.7  20.7  111.8
16  17.0               Utah Jazz*  82  240.9  40.4  ...  8.1  5.9  15.1  21.1  111.7
17  18.0       San Antonio Spurs*  82  241.5  42.3  ...  6.1  4.7  12.1  18.1  111.7
18  19.0        Charlotte Hornets  82  241.8  40.2  ...  7.2  4.9  12.2  18.9  110.7
19  20.0          Denver Nuggets*  82  240.6  41.9  ...  7.7  4.4  13.4  20.0  110.7
20  21.0         Dallas Mavericks  82  241.2  38.8  ...  6.5  4.3  14.2  20.1  108.9
21  22.0          Indiana Pacers*  82  240.3  41.3  ...  8.7  4.9  13.7  19.4  108.0
22  23.0             Phoenix Suns  82  242.4  40.1  ...  9.0  5.1  15.6  23.6  107.5
23  24.0           Orlando Magic*  82  241.2  40.4  ...  6.6  5.4  13.2  18.6  107.3
24  25.0         Detroit Pistons*  82  242.1  38.8  ...  6.9  4.0  13.8  22.1  107.0
25  26.0               Miami Heat  82  240.6  39.6  ...  7.6  5.5  14.7  20.9  105.7
26  27.0            Chicago Bulls  82  242.7  39.8  ...  7.4  4.3  14.1  20.3  104.9
27  28.0          New York Knicks  82  241.2  38.2  ...  6.8  5.1  14.0  20.9  104.6
28  29.0      Cleveland Cavaliers  82  240.9  38.9  ...  6.5  2.4  13.5  20.0  104.5
29  30.0        Memphis Grizzlies  82  242.4  38.0  ...  8.3  5.5  14.0  22.0  103.5
30   NaN           League Average  82  241.6  41.1  ...  7.6  5.0  14.1  20.9  111.2

[31 rows x 25 columns]
chitown88
  • 27,527
  • 4
  • 30
  • 59