Skip to content

Instantly share code, notes, and snippets.

@henryeleonu
Created December 16, 2022 11:47
Show Gist options
  • Save henryeleonu/c398d95b3eabb64b7f28d891a4d44952 to your computer and use it in GitHub Desktop.
Save henryeleonu/c398d95b3eabb64b7f28d891a4d44952 to your computer and use it in GitHub Desktop.
How can I scrape a table from a website in python
import requests
from bs4 import BeautifulSoup
# Make a request to the website
url = "http://www.example.com/table.html"
r = requests.get(url)
# Parse the HTML content
soup = BeautifulSoup(r.content, "html.parser")
# Find the table in the HTML
table = soup.find("table")
# Loop through the rows of the table
for row in table.find_all("tr"):
# Loop through the cells of the row
cells = row.find_all("td")
if cells:
# Extract the text from the cells
cell_values = [cell.text for cell in cells]
print(cell_values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment