How to export an HTML table to a CSV file

You found the data you need in a table on a web page, and you want it as a CSV file you can open in Excel, Sheets or a script. These are the four usual routes, in order of effort.

1. Copy, paste, save

Select the table, copy it, paste it into a spreadsheet and use File, then Save As CSV (Excel) or File, Download, Comma-separated values (Sheets). It is quick, and it fails on tables built from div elements or with merged cells.

2. A spreadsheet formula

In Google Sheets, =IMPORTHTML("https://example.com/page", "table", 1) pulls the first table from a public page. Change the 1 to pick another table. It only works on pages that do not need a login and that load their tables without JavaScript.

3. A short console script

Open the page, press F12, open the Console tab and paste this. It downloads the first table on the page as table.csv. Change querySelector('table') to querySelectorAll('table')[2] for the third table. Quotes inside cells are escaped, so commas in the text are safe.

(() => {
  const t = document.querySelector('table');            // first table on the page
  const rows = [...t.rows].map(r => [...r.cells].map(c => '"' + c.innerText.trim().replace(/"/g, '""') + '"').join(','));
  const csv = rows.join('\n');
  const a = document.createElement('a');
  a.href = URL.createObjectURL(new Blob([csv], { type: 'text/csv' }));
  a.download = 'table.csv'; a.click();
})();

Chrome may ask you to type "allow pasting" the first time you paste into the console.

4. An extension

Table to CSV & Excel – Table Copier lists every table on the page, outlines the one you pick, and gives you Copy or Download in one click. See the help page.

Check the result

Related: How to copy a table from a website into Excel or Google Sheets.