🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

pd.read_html()

AI & DATA SCIENCE // pd-read-html

pd.read_html() scans an HTML document (a file, URL, or raw string) for <table> elements and returns each one as a separate DataFrame in a list.

Syntax

pd.read_html(io, match='.+', attrs=None)

Deep Dive Course

read_html() parses the HTML looking specifically for table tags and attempts to convert each one into a DataFrame, returning a list since a page can contain multiple tables — even if you expect only one, the result is still a list, and you typically index into it to get the table you want. The match parameter, a string or regex, filters down to only tables containing matching text, which is useful for pages with several unrelated tables, and attrs lets you target a table by its HTML attributes, like a specific id or class.

1Understanding pd.read_html()

read_html() parses the HTML looking specifically for table tags and attempts to convert each one into a DataFrame, returning a list since a page can contain multiple tables — even if you expect only one, the result is still a list, and you typically index into it to get the table you want. The match parameter, a string or regex, filters down to only tables containing matching text, which is useful for pages with several unrelated tables, and attrs lets you target a table by its HTML attributes, like a specific id or class.

💡

read_html() always returns a list of DataFrames, even for a page with exactly one table — a common mistake is treating the result as a single DataFrame directly instead of indexing into the list first.

editor.html
import pandas as pd

tables = pd.read_html("https://example.com/stats")
print(len(tables))
print(tables[0].head(2))
localhost:3000

2Practical Example

Here is a real-world application of pd.read_html() showing how it is used in production Pandas code.

editor.html
import pandas as pd

tables = pd.read_html("https://example.com/stats", match="Score")
df = tables[0]
print(df.shape)
localhost:3000

3Best Practices

Follow these guidelines when working with pd.read_html():

1. Remember read_html() always returns a list — index into it, typically at position 0, to get a specific table as an actual DataFrame

2. Use the match parameter to narrow down to the relevant table on pages with multiple unrelated tables, instead of guessing at a list index

3. Expect to do follow-up cleanup, dtype conversion, dropping unwanted header/footer rows, after read_html(), since HTML tables are rarely as cleanly structured as a proper CSV export

⚠️

Tip: read_html() always returns a list of DataFrames, even for a page with exactly one table — a common mistake is treating the result as a single DataFrame directly instead of indexing into the list first.

editor.html
import pandas as pd

tables = pd.read_html("https://example.com/stats")
print(len(tables))
print(tables[0].head(2))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

tables = pd.read_html("https://example.com/stats")
print(len(tables))
print(tables[0].head(2))
Example 02Advanced Example
import pandas as pd

tables = pd.read_html("https://example.com/stats", match="Score")
df = tables[0]
print(df.shape)

Best Practices

  • Remember read_html() always returns a list — index into it, typically at position 0, to get a specific table as an actual DataFrame
  • Use the match parameter to narrow down to the relevant table on pages with multiple unrelated tables, instead of guessing at a list index
  • Expect to do follow-up cleanup, dtype conversion, dropping unwanted header/footer rows, after read_html(), since HTML tables are rarely as cleanly structured as a proper CSV export

Interview Question

Why does read_html() return a list of DataFrames even when the source page contains only a single table?

Hint: Think about what read_html() can't know in advance about the page's structure.

read_html() scans the entire document for every table element it can find, and it has no way of knowing in advance how many tables a given page contains, whether that's zero, one, or many — so it consistently returns a list to handle every possible case uniformly, rather than special-casing the single-table scenario to return a bare DataFrame. This means even when you're confident a page has exactly one table, you still need to index into the returned list to get an actual usable DataFrame out of it.

Exercises

MediumPractice using pd.read_html() in a real scenario.
View Solution
import pandas as pd

tables = pd.read_html("https://example.com/stats")
print(len(tables))
print(tables[0].head(2))

Frequently Asked Questions

Why does read_html() return a list of DataFrames even when the source page contains only a single table?

read_html() scans the entire document for every table element it can find, and it has no way of knowing in advance how many tables a given page contains, whether that's zero, one, or many — so it consistently returns a list to handle every possible case uniformly, rather than special-casing the single-table scenario to return a bare DataFrame. This means even when you're confident a page has exactly one table, you still need to index into the returned list to get an actual usable DataFrame out of it.

Related Functions

pd-read-csvpd-read-jsondf-to-csv