Skip to content

Instantly share code, notes, and snippets.

@henryeleonu
Created December 16, 2022 11:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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