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 BeautifulSoup HEADERS = {"User-Agent": "Mozilla/5.0 (compatible; StarterKitBot/1.0)"} def table_to_csv(url, output_path, table_index=0): response = requests.get(url, headers=HEADERS, timeout=15) response.raise_for_status() soup = BeautifulSoup(response.text, "lxml") tables = soup.find_all("table") if not tables: print("No found. The data might be loaded by JavaScript.") return rows = [] for tr in tables[table_index].find_all("tr"): cells = tr.find_all(["th", "td"]) rows.append([cell.get_text(" ", strip=True) for cell in cells

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 BeautifulSoup

HEADERS = {"User-Agent": "Mozilla/5.0 (compatible; StarterKitBot/1.0)"}

def table_to_csv(url, output_path, table_index=0):
    response = requests.get(url, headers=HEADERS, timeout=15)
    response.raise_for_status()

    soup = BeautifulSoup(response.text, "lxml")
    tables = soup.find_all("table")
    if not tables:
        print("No  found. The data might be loaded by JavaScript.")returnrows=[]fortrintables[table_index].find_all("tr"):cells=tr.find_all(["th","td"])rows.append([cell.get_text(" ",strip=True)forcellincells])withopen(output_path,"w",newline="",encoding="utf-8")asf:csv.writer(f).writerows(rows)print(f"Saved {len(rows)} rows to {output_path}")table_to_csv("https://example.com/prices","prices.csv")

Run it:

pip install requests beautifulsoup4 lxml
python table_to_csv.py

Open prices.csv in Excel or Google Sheets and you are done.

The parts that actually trip people up

The scraping itself is the easy part. The things that eat your afternoon are:

  • Pagination — the data you want is spread across 20 pages, and each page needs the same treatment.
  • Cleaning the output — merged cells, stray whitespace, prices stored as text with currency symbols.
  • Picking the right table — pages often have more than one
. The --index flag in the full version lets you choose which one.
  • JavaScript-loaded pages — if find_all("table") comes back empty, the table probably renders in the browser after load, and requests alone will not see it.
  • A polite User-Agent, a short delay between pages, and a quick look at the site's robots.txt keep you out of most trouble on public pages.

    Where this goes next

    The full version of this script (with the --index flag and proper error messages) is free on my GitHub: https://github.com/luciusdata/table-to-csv

    I packaged the versions I actually reuse — table-to-CSV, multi-page listings, product prices, business contacts, a sitemap sweep, JSON API-to-CSV, and a CSV cleaner — into a small starter kit with a plain-English guide. It is $3 while the store is new: https://luciusdata.gumroad.com/l/sbpiem

    Happy to answer questions about the approach in the comments, whether you grab the kit or not.

    Share

    phult

    Phu.lt chia sẻ kiến thức về công nghệ và lập trình, từ phát triển phần mềm, DevOps, Linux và kiến trúc hệ thống đến những diễn biến mới trong khoa học và công nghệ. Mỗi bài viết hướng đến việc giải thích rõ cách công nghệ hoạt động, giá trị thực tế và những giới hạn cần biết.

    This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies Find out more here