I need to extract some data from a web page and store it in a CSV file to be opened in Excel. I am trying to do this from the browser console, but I am getting a non-defined error.
So far I've tried using this code to export the table:
function exportTableToCSV(filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for (var j = 0; j < cols.length; j++)
row.push(cols[j].innerText);
csv.push(row.join(","));
}
// Download CSV file
downloadCSV(csv.join("\n"), filename);
}
When I define this function and run it, I get a "downloadCSV not defined error". Are there any limitations to the built in Browser console in Chrome, besides the obvious ones?
This code also needs to be run in a loop, since there are multiple pages with tables, but it would be nice to have it all located in a single CSV file. For starters it should really just extract anything it can find in the table. I'll dive in and extract the specific fields I need later when I get this working.
Edit:
function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;
csvFile = new Blob([csv], {
type: "text/csv"
});
downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}