Web Scraping Sports Stats with Python | Extract Data from Baseball Reference

Опубликовано: 01 Декабрь 2025
на канале: JR: Educational Channel
265
10

YouTube Video Description:
Want to analyze sports statistics with Python? This tutorial demonstrates how to scrape player stats from Baseball Reference using pandas, requests, and BeautifulSoup. This method works for all Sports Reference websites, including basketball, football, and hockey!

What You’ll Learn:
How to extract player statistics from Baseball Reference
Using requests and BeautifulSoup for web scraping
Converting HTML tables into Pandas DataFrames
Filtering season data for better analysis

Code from Tutorial:
***
import pandas as pd
import requests
from bs4 import BeautifulSoup

Define the URL for a player's statistics
url = 'https://www.baseball-reference.com/pl...

Send request and parse HTML
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

Extract the third table on the page (index 2)
df = pd.read_html(str(soup.find_all('table')[2]))[0]

Filter data to display only seasons (exclude other rows)
df[df['Season'].astype(str).str.len() == 4]
***

Why This is Useful?
Works on all Sports Reference sites (NBA, NFL, NHL, etc.)
Automates data collection for analysis & visualization
Saves time from manually copying statistics
Useful for fantasy sports, betting, and sports analytics