HTML Tables: Web Scraping Basics
You'll learn how to extract tabular data directly from web pages using Pandas' built-in HTML parser.
The Core Idea
Many websites display data in HTML <table> tags—think Wikipedia tables, financial reports, or sports statistics. Instead of manually copying data, Pandas can automatically find and parse these tables into DataFrames with pd.read_html(). It's like having a robot assistant that scans a webpage and hands you ready-to-analyze spreadsheets.
How pd.read_html() Works
This function takes a URL or HTML string and returns a list of DataFrames—one for each table found on the page:
import pandas as pd
# Extract all tables from a webpage
tables = pd.read_html('https://example.com/data-page')
# Returns a list - even if there's only one table
print(f"Found {len(tables)} tables")
# Access the first table
df = tables[0]
Handling Multiple Tables
When a page contains several tables, you'll need to identify which one you want by its index position:
# Get the third table on the page
sales_data = tables[2]
# Or loop through all tables to inspect them
for i, table in enumerate(tables):
print(f"Table {i} shape: {table.shape}")
Basic Parsing Options
Fine-tune extraction with key parameters:
# Specify which table by matching text content
tables = pd.read_html(url, match='Revenue')
# Set the header row (if not first row)
tables = pd.read_html(url, header=1)
# Skip rows at the top
tables = pd.read_html(url, skiprows=2)
Key Takeaway: pd.read_html() automatically extracts all HTML tables from a URL or HTML string into a list of DataFrames, with options to filter, select headers, and handle multi-table pages—making web data instantly analyzable.