Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74297

How to scrape data from last table on website

$
0
0

I'm trying to scrape the "team per game stats" table from this website using this code:

from urllib.request import urlopen as uo
from bs4 import BeautifulSoup as BS
import pandas as pd

url = 'https://www.basketball-reference.com/leagues/NBA_2020.html'
html = uo(url)
soup = BS(html, 'html.parser')
soup.findAll('tr')

headers = [th.getText() for th in soup.findAll('tr')]
headers = headers[1:]
print(headers)

rows = soup.findAll('tr')[1:]
team_stats = [[td.getText() for td in rows[i].findAll('td')]
                for i in range(len(rows))]

stats = pd.DataFrame(team_stats, columns=headers)

But it returns this error:

AssertionError: 71 columns passed, passed data had 212 columns

Viewing all articles
Browse latest Browse all 74297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>