I got tired of copy-pasting website tables into Excel, so I automated it with 30 lines of Python
If you have ever spent an afternoon copying a product table off a website row by row, this is the fix. You only need two libraries: requests to fetch the page, and BeautifulSoup to pull the table out. Find the table, loop over its rows, write each row to a CSV file — that is genuinely most of the job for public, static pages. The whole script import csv import requests from bs4 import…
Transcribing data from websites into Excel spreadsheets can quickly become a tedious task. To streamline this process, someone developed a Python script that automates the extraction of tables from web pages and converts them into CSV files. The script requires two external libraries: requests, which fetches the webpage content, and BeautifulSoup, which parses the HTML to locate and extract the desired table.
The core functionality of the script involves specifying the URL of the webpage containing the table, the desired output file path for the CSV file, and an optional table index to select which table to process if the page contains multiple tables. The script sends a GET request to the specified URL with a custom User-Agent header to mimic a web browser, ensuring compatibility with most websites. Upon receiving the response, BeautifulSoup parses the HTML content and identifies all tables present.
The script then proceeds to extract each row of data from the selected table. For each row, it gathers the cell values from both header (th) and data (td) cells. These cell values are cleaned and stored in a list, representing a single row of data. After looping through all rows in the specified table, the script writes this data to a CSV file using Python's built-in csv module. The script concludes by printing a confirmation message indicating the number of rows saved to the output file.
While the actual data extraction process is straightforward, there are a few challenges that can complicate the task. Websites often present data across multiple pages, requiring the script to handle pagination. Additionally, some data might be messy, necessitating cleaning steps such as removing merged cells, eliminating stray whitespace, and handling prices that are stored as text with currency symbols.
Furthermore, not all websites are static; certain tables may be loaded using JavaScript after the page has finished loading, rendering them invisible to simple scraping methods like requests. In such cases, employing a headless browser or JavaScript execution capabilities would be necessary.
To address these challenges, the script includes features like a pagination flag for handling multiple pages, a cleaner for processing messy data, and an index flag to select specific tables when multiple tables are present on a page. Moreover, the script incorporates a polite User-Agent header to adhere to website usage policies, adds brief delays between requests to avoid overloading servers, and checks the website's robots.txt file to ensure compliance with their scraping policies.
For those interested in using or improving upon this script, the full version complete with additional features and error handling is available on GitHub. The author has also packaged various adaptations of the script, including solutions for pagination, multi-page listings, product prices, business contacts, and more, into a starter kit available for purchase. This starter kit comes with a comprehensive guide to help users navigate and customize the tools to suit their specific needs.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to