I'm trying to scrape the main table on this webpage, but my code is returning an error:
AttributeError: 'NoneType' object has no attribute 'find_all'
The website is: https://www.rotowire.com/daily/nba/optimizer.php?site=FanDuel
Here is the code, any help appreciated!
import requests
from bs4 import BeautifulSoup
import pandas as pd
# Request the page
url = "https://www.rotowire.com/daily/nba/optimizer.php?site=FanDuel"
page = requests.get(url)
# Use BeautifulSoup to parse the page
soup = BeautifulSoup(page.content, "html.parser")
# Find the table on the page
table = soup.find("table", attrs={"class": "table-stats"})
# Extract the data from the table
data = []
rows = table.find_all("tr")
for row in rows:
cols = row.find_all("td")
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
# Convert the data to a pandas DataFrame
df = pd.DataFrame(data[1:], columns=data[0])
# Check if the file exists, and if so, read in the existing data
try:
existing_df = pd.read_csv("nba_optimizer.csv")
df = pd.concat([existing_df, df])
except FileNotFoundError:
pass
# Write the DataFrame to a CSV file
df.to_csv("nba_optimizer.csv", index=False, mode="a", header=not bool(existing_df))