Skip to content

Instantly share code, notes, and snippets.

@raspberrypisig
Last active November 1, 2025 03:45
Show Gist options
  • Select an option

  • Save raspberrypisig/d3cb39e798ddbd20a4ca3d2ab549da2f to your computer and use it in GitHub Desktop.

Select an option

Save raspberrypisig/d3cb39e798ddbd20a4ca3d2ab549da2f to your computer and use it in GitHub Desktop.
from bs4 import BeautifulSoup
def bs4demo():
html = """
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>30</td>
</tr>
<tr>
<td>Bob</td>
<td>25</td>
</tr>
</table>
"""
soup = BeautifulSoup(html, "html.parser")
rows = soup.select("tr")
for row in rows:
cells = [td.text for td in row.select("td")]
if len(cells) > 0:
print(cells)
if __name__ == "__main__":
bs4demo()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>30</td>
</tr>
<tr>
<td>Bob</td>
<td>25</td>
</tr>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment