π NBA Stats Scraper (2020β2024)
A Python script that scrapes NBA player statistics (per game, totals, advanced) from Basketball Reference using Playwright + BeautifulSoup + Pandas and saves them into a single CSV file.
π Features
π Scrapes per game, totals, and advanced stats
π Covers multiple seasons (2020β2024)
π Retry mechanism with delays to handle failed requests
β³ Progress bar with tqdm for tracking scraping progress
π₯ Headless Chromium browser automation with Playwright
πΎ Saves results to nba_stats.csv
π Tech Stack
Python 3
Libraries:
playwright β headless browser automation
beautifulsoup4 β parse HTML & comments (hidden tables)
pandas β tabular data processing & CSV export
tqdm β progress bar
io.StringIO, time.sleep β retries & parsing support
βοΈ Setup & Installation 1οΈβ£ Clone the repository git clone https://github.com/DebanilBora/NBA-Stats-Scraper.git cd nba-stats-scraper
2οΈβ£ Create & activate a virtual environment python -m venv .venv source .venv/bin/activate # Linux/Mac .venv\Scripts\activate # Windows
3οΈβ£ Install dependencies pip install -r requirements.txt
4οΈβ£ Install Playwright browsers playwright install
Run the script:
python app.py
β Opens Chromium in headless mode
β Scrapes NBA player stats for 2020β2024 seasons
β Extracts per game, totals, advanced stats
β Saves final dataset into nba_stats.csv
π Example Output (CSV) Player Team G PTS TRB AST Season StatType LeBron James LAL 67 25.3 7.8 10.2 2020 per_game Giannis Ant. MIL 63 28.1 11.0 5.9 2020 advanced π· Tags
#Python #WebScraping #NBA #Basketball #DataScience #Playwright #BeautifulSoup #Pandas