How to scrape tables with BeautifulSoup?

We can parse a table’s content with BeautifulSoup by finding all

elements, and finding their

or

children.

Here is an example on how to parse this demo table using BeautifulSoup:

import requests
from bs4 import BeautifulSoup

response = requests.get("https://demo.scrapingbee.com/table_content.html")
soup = BeautifulSoup(response.content, 'html.parser')

data = []
table = soup.find('table')
table_body = table.find('tbody')

rows = table.find_all('tr')
for row in rows:
    cols = row.find_all(['td', 'th'])
    cols = [ele.text.strip() for ele in cols]
    data.append([ele for ele in cols if ele])
print(data)

 

Related BeautifulSoup web scraping questions:

Tired of getting blocked while scraping the web?

ScrapingBee API handles headless browsers and rotates proxies for you.

Get access to 1,000 free API credits, no credit card required!

ScrapingBee API handles headless browsers and rotates proxies for you.

Copyright © 2025

Made in France

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *