Scrape a Table From a Website and Save it as an Excel File

Scrape a Table From a Website and Save it as an Excel File

Web scraping is the process of extracting data from websites. Here is a sample Python script that uses the requests and BeautifulSoup libraries to scrape a table from a website, and then uses the pandas library to save the table as an Excel file.

First, you will have to paste the link from the website where the table is located, and then the table name. Right click → inspect on the table and you can easily find the table name.

The Excel file will be saved to the same directory as the python file with the name of the table.

import requests
from bs4 import BeautifulSoup
import pandas as pd

# Make a request to the website
url = input('Link from the website: ')
table_name = input('Name of the table: ')
response = requests.get(url)

# Parse the HTML content
soup = BeautifulSoup(response.content, 'html.parser')

# Find the table with the ID equal to the table_name
table = soup.find('table', {'id': table_name})

# Use pandas to convert the table to a DataFrame
df = pd.read_html(str(table))[0]

# Save the DataFrame as an Excel file.
df.to_excel(table_name+'.xlsx', index=False)

Leave a Reply

Prev
Penetration Testing Techniques and Strategies
Penetration Testing Techniques and Strategies

Penetration Testing Techniques and Strategies

Whenever a white hat hacker is approaching a penetration test, they should

Next
Python Lambda Functions with Practical Examples
Python Lambda Functions with Practical Examples

Python Lambda Functions with Practical Examples

A lambda function in Python is a small anonymous function that is defined using

You May Also Like