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)