How can I scrape a table from a website in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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