Skip to content
Start free
Back to Blog

How to Scrape HTML Table in JavaScript and Export Clean CSV

Mihai MaximLast updated on 11 min read
How to Scrape HTML Table in JavaScript and Export Clean CSV
TL;DR: First determine whether the table rows exist in the HTTP response, arrive through a data request, or require a browser to render. Then use the static Axios and Cheerio script or the focused Puppeteer path below to scrape HTML table in JavaScript, validate header-driven records, deduplicate pages, and write CSV safely. An HTML table organizes information into rows and columns, usually with <table>, <tr>, <th>, and <td> elements. To scrape HTML table in JavaScript means retrieving or rendering that markup, converting each data row into a JavaScript object, validating the result, and serializing it to a format such as CSV.

The loop over rows is rarely the difficult part. Most failures happen because the scraper reads the server response while the developer inspected a browser-rendered DOM, targets the wrong table, assumes fixed cell positions, or exports unescaped values. A reliable workflow chooses the lightest extraction method that can actually see the data.

This Node.js web scraping tutorial starts with Axios and Cheerio for static markup, then adds a focused Puppeteer table scraping path for rows created after page load. The parser derives keys from table headings rather than assuming one dataset. It also reports malformed rows, preserves values as strings until conversion is intentional, and checks that the output is usable. The result is a practical way to scrape HTML table in JavaScript without pretending that rowspan, colspan, nested tables, or every custom grid can be flattened automatically.

Choose the right extraction path before writing code

Before you scrape HTML table in JavaScript, identify where the row data exists. In standard markup, <tr> represents a row, <th> usually labels a column, and <td> holds a regular value. MDN's HTML table element reference is a useful semantic check when the structure is unfamiliar.

Use this decision order:

  1. Rows in the response HTML: request the page with Axios and parse it as shown in our web scraping with Cheerio guide.
  2. Rows returned by Fetch or XHR: use the permitted data endpoint when it is stable and appropriate.
  3. Rows created only after scripts or interaction: use browser automation.

This order keeps a JavaScript table scraper simpler and cheaper to operate than starting with a browser for every page.

Confirm whether the rows are present in the response HTML

Open View Source, not only the Elements panel, and search for a distinctive cell value. You can also save response.data and search it directly. If the value and a scoped row selector are present, browser automation is unnecessary, even when the page displays pagination controls.

Test selectors in DevTools before sending repeated requests, then retest them against fetched markup. A CSS selectors cheat sheet helps when table tr also captures navigation, nested tables, or footer rows.

Check for network-fetched or browser-rendered rows

If the value is absent from source, filter the Network panel for Fetch/XHR, reload, change a page or filter, and inspect the responses. A permitted, stable data endpoint is usually easier to validate than rendered cells, but confirm that using it is allowed.

Choose a headless browser when JavaScript, login state, clicking, or scrolling is essential. A Cheerio versus Puppeteer comparison can help document that tradeoff. Avoid regular expressions as a general HTML parser; use them only for narrow patterns inside an already selected cell.

Build a reusable Axios and Cheerio scraper to scrape HTML table in JavaScript

For a static page, the pipeline is straightforward: Axios retrieves the response, Cheerio loads the markup, and a scoped parser turns rows into objects. The important design choice is configuration. Accept the URL, table selector, and output path as inputs, then derive record keys from the selected table's headings.

This Axios Cheerio web scraping approach avoids a brittle mapping such as “cell zero is name, cell one is price.” It works across conventional tables whose headings and row widths are consistent, while still surfacing structural exceptions.

Set up the Node.js project and request the page safely

Create a project and install the three packages used by the static example:

mkdir table-scraper
cd table-scraper
npm init -y
npm install axios cheerio objects-to-csv

Create scrape-table.js. The complete script uses CommonJS consistently, applies a request timeout, accepts only successful HTTP statuses, and wraps request, parsing, validation, and export work in one error boundary. It does not blindly retry client errors, because repeated requests will not repair a missing selector, denied access, or an invalid URL.

Package APIs can change. Pin versions after testing the script in your environment, and review the official package documentation before treating the example as a production dependency.

Select the table and derive stable column names

Scope the selector to one table whenever possible, for example #results rather than table. The parser takes the first match deliberately, then looks for the last row in <thead>. If there is no <thead>, it treats the first row containing <th> cells as the header. If labels are still unavailable, it generates column_1, column_2, and so on.

Heading text is normalized to lowercase snake case. A blank heading receives a positional fallback, while duplicates become keys such as price and price_2. That prevents later cells from overwriting earlier properties. For multilingual headings or a public data contract, replace automatic normalization with an explicit header map.

Multi-row headings, rowspan, and colspan require table-specific logic because the visual grid may not match the DOM cell count. Do not silently guess. Inspect the markup, define the intended columns, and add a grid-expansion step if those semantics matter.

Map each row to an object without hard-coded field names

Read the direct child cells of each row rather than every descendant cell. That distinction prevents a nested table from adding unexpected values to its parent row. The script first builds a matrix of cleaned cell strings, skips rows that contain no meaningful text, and calculates the widest observed row.

It then creates one key for each column and pairs keys with values by index. Missing values become empty strings. Extra cells receive generated headings when the initial scan reveals them. The parser also records a warning whenever a row's cell count differs from the computed width, so shifted columns do not pass unnoticed.

This is the reusable core when you scrape HTML table in JavaScript: the destination array lives outside row iteration, and every accepted row contributes one complete object. It is still important to inspect warnings. A width mismatch can indicate a legitimate spanning cell, a hidden control column, malformed markup, or a selector that crossed into another table section.

Normalize and validate the extracted values

Normalize presentation noise, not meaning. The example converts nonbreaking spaces to regular spaces, trims the ends, and collapses repeated whitespace. It leaves dates, currencies, percentages, identifiers, and leading zeros as strings because values such as 01/02/03 or 1,234 are ambiguous without locale and schema rules.

Validation should fail loudly when the table selector is missing or no nonempty records remain. Also verify required columns, inspect a sample object, review mismatch warnings, and confirm that the final file exists before a downstream job consumes it. If you need typed values, apply explicit per-column converters after extraction and retain the raw value for debugging. A data parsing guide is a useful companion when those conversions become a separate pipeline stage.

Serialize the records to CSV safely

Do not build CSV with row.join(","). Commas, double quotes, carriage returns, and line breaks inside a cell require escaping and quoting. Use an actively maintained serializer whose behavior you have tested. The example uses objects-to-csv, gives every object properties in the same header order, and writes the resulting JavaScript array to CSV.

Test the chosen version with fixtures containing commas, quotes, embedded newlines, blank values, and non-ASCII text. Confirm its header and UTF-8 behavior in the applications that will read the file. Correct CSV serialization cannot repair an irregular source table; resolve spanning cells, duplicated semantics, and mismatched rows before export.

Run the complete static-table scraper

Save the following as scrape-table.js. It covers request, selection, header normalization, row mapping, checks, and CSV export. Edit requiredColumns when downstream data needs guaranteed fields.

const axios = require("axios");
const cheerio = require("cheerio");
const ObjectsToCsv = require("objects-to-csv");

const [url, tableSelector = "table", outputPath = "table.csv"] =
  process.argv.slice(2);

const requiredColumns = [];

function clean(value) {
  return value
    .replace(/\u00a0/g, " ")
    .replace(/\s+/g, " ")
    .trim();
}

function uniqueHeaders(labels, width) {
  const seen = new Map();

  return Array.from({ length: width }, (_, index) => {
    const normalized = clean(labels[index] || "")
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "_")
      .replace(/^_+|_+$/g, "");

    const base = normalized || `column_${index + 1}`;
    const count = (seen.get(base) || 0) + 1;
    seen.set(base, count);

    return count === 1 ? base : `${base}_${count}`;
  });
}

function parseTable(html) {
  const $ = cheerio.load(html);
  const table = $(tableSelector).first();

  if (!table.length) {
    throw new Error(`No table matched selector: ${tableSelector}`);
  }

  let headerRow = table.find("thead tr").last();

  if (!headerRow.length) {
    const firstRow = table.find("tr").first();
    if (firstRow.children("th").length) {
      headerRow = firstRow;
    }
  }

  const headerLabels = headerRow
    .children("th, td")
    .map((_, cell) => clean($(cell).text()))
    .get();

  let dataRows = table.children("tbody").children("tr");
  if (!dataRows.length) {
    dataRows = table.children("tr");
  }

  const headerNode = headerRow.get(0);
  const matrix = dataRows
    .toArray()
    .filter((row) => row !== headerNode)
    .map((row) =>
      $(row)
        .children("th, td")
        .map((_, cell) => clean($(cell).text()))
        .get()
    )
    .filter((cells) => cells.some(Boolean));

  const width = Math.max(
    0,
    headerLabels.length,
    ...matrix.map((cells) => cells.length)
  );

  if (!width) {
    throw new Error("The selected table has no readable cells.");
  }

  const headers = uniqueHeaders(headerLabels, width);
  const warnings = [];

  if (headerLabels.length && headerLabels.length !== width) {
    warnings.push(
      `Header has ${headerLabels.length} cells; widest row has ${width}.`
    );
  }

  const records = matrix.map((cells, rowIndex) => {
    if (cells.length !== width) {
      warnings.push(
        `Row ${rowIndex + 1} has ${cells.length} cells; expected ${width}.`
      );
    }

    return Object.fromEntries(
      headers.map((header, cellIndex) => [
        header,
        cells[cellIndex] ?? "",
      ])
    );
  });

  return { headers, records, warnings };
}

async function main() {
  if (!url) {
    throw new Error(
      'Usage: node scrape-table.js "<url>" "<table-selector>" "<output.csv>"'
    );
  }

  const response = await axios.get(url, {
    timeout: 15000,
    responseType: "text",
    maxRedirects: 5,
    validateStatus: (status) => status >= 200 && status < 300,
    headers: {
      "User-Agent": "TableScraper/1.0 (+contact@example.com)",
    },
  });

  const { headers, records, warnings } = parseTable(response.data);

  if (!records.length) {
    throw new Error("The table matched, but no nonempty rows were parsed.");
  }

  for (const column of requiredColumns) {
    if (!headers.includes(column)) {
      throw new Error(`Required column is missing: ${column}`);
    }
  }

  if (warnings.length) {
    console.warn(warnings.slice(0, 10).join("\n"));
  }

  await new ObjectsToCsv(records).toDisk(outputPath);

  console.log(`Wrote ${records.length} rows to ${outputPath}`);
  console.log(`Columns: ${headers.join(", ")}`);
  console.log("Sample record:", records[0]);
}

main().catch((error) => {
  console.error(`Scrape failed: ${error.message}`);
  process.exitCode = 1;
});

Run it with the target URL, a quoted table selector, and an output path:

node scrape-table.js "https://example.com/page" "#results" "results.csv"

Inspect the reported columns and sample record before trusting the CSV. Treat every width warning as a data-quality issue to investigate.

Handle JavaScript-rendered and paginated tables

To scrape HTML table in JavaScript when rows are absent from the response, use browser automation after ruling out an appropriate data endpoint. A headless browser executes page scripts and can click controls, but it consumes more memory and introduces more failure states than an HTTP request.

The focused path below uses Puppeteer and a configurable Next control. Install it in the same project:

npm install puppeteer

This path is suitable for a conventional rendered table. It does not attempt to solve virtualized grids that recycle DOM nodes, authentication challenges, or arbitrary component frameworks without selector changes.

Wait for rows and collect every page or scroll batch

Wait for a row selector rather than sleeping for an arbitrary duration. On each page, extract a cell matrix, add its records, click Next, and wait until the matrix changes. The official Puppeteer waitForSelector documentation describes the selector wait used for the first batch.

Save this as scrape-rendered.js. The code reuses the header strategy, fails on shifted columns, and refuses to write a file when pagination appears stuck.

const puppeteer = require("puppeteer");
const ObjectsToCsv = require("objects-to-csv");

const [
  url,
  tableSelector = "#results",
  nextSelector = 'button[aria-label="Next"]',
  outputPath = "rendered.csv",
] = process.argv.slice(2);

function clean(value) {
  return value.replace(/\u00a0/g, " ").replace(/\s+/g, " ").trim();
}

function uniqueHeaders(labels, width) {
  const seen = new Map();

  return Array.from({ length: width }, (_, index) => {
    const normalized = clean(labels[index] || "")
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "_")
      .replace(/^_+|_+$/g, "");

    const base = normalized || `column_${index + 1}`;
    const count = (seen.get(base) || 0) + 1;
    seen.set(base, count);
    return count === 1 ? base : `${base}_${count}`;
  });
}

async function main() {
  if (!url) {
    throw new Error(
      'Usage: node scrape-rendered.js "<url>" "<table>" "<next>" "<output.csv>"'
    );
  }

  const rowSelector = `${tableSelector} tbody tr`;
  const headerSelector =
    `${tableSelector} thead tr:last-child th, ` +
    `${tableSelector} thead tr:last-child td`;

  const browser = await puppeteer.launch({ headless: true });

  try {
    const page = await browser.newPage();
    await page.goto(url, {
      waitUntil: "domcontentloaded",
      timeout: 30000,
    });
    await page.waitForSelector(rowSelector, { timeout: 15000 });

    const labels = await page.$$eval(headerSelector, (cells) =>
      cells.map((cell) => cell.textContent.replace(/\s+/g, " ").trim())
    );

    const recordsByValue = new Map();
    let headers;
    let pageNumber = 0;

    while (true) {
      pageNumber += 1;

      if (pageNumber > 1000) {
        throw new Error("Safety limit reached before pagination completed.");
      }

      const matrix = await page.$$eval(rowSelector, (rows) =>
        rows.map((row) =>
          Array.from(row.children)
            .filter((cell) => cell.matches("th, td"))
            .map((cell) => cell.textContent.replace(/\s+/g, " ").trim())
        )
      );

      if (!matrix.length) {
        throw new Error(`No rows found on rendered page ${pageNumber}.`);
      }

      if (!headers) {
        const width = Math.max(
          labels.length,
          ...matrix.map((cells) => cells.length)
        );
        headers = uniqueHeaders(labels, width);
      }

      for (const cells of matrix) {
        if (cells.length !== headers.length) {
          throw new Error(
            `Column mismatch on page ${pageNumber}: ` +
              `${cells.length} cells, expected ${headers.length}.`
          );
        }

        const record = Object.fromEntries(
          headers.map((header, index) => [header, cells[index]])
        );

        recordsByValue.set(JSON.stringify(record), record);
      }

      const next = await page.$(nextSelector);
      if (!next) break;

      const disabled = await next.evaluate(
        (element) =>
          element.disabled ||
          element.getAttribute("aria-disabled") === "true" ||
          element.classList.contains("disabled")
      );

      if (disabled) break;

      const previous = JSON.stringify(matrix);
      await next.click();

      try {
        await page.waitForFunction(
          ({ rowSelector, previous }) => {
            const current = Array.from(
              document.querySelectorAll(rowSelector)
            ).map((row) =>
              Array.from(row.children)
                .filter((cell) => cell.matches("th, td"))
                .map((cell) =>
                  cell.textContent.replace(/\s+/g, " ").trim()
                )
            );

            return current.length > 0 && JSON.stringify(current) !== previous;
          },
          { timeout: 10000 },
          { rowSelector, previous }
        );
      } catch {
        throw new Error(
          `Pagination did not advance after page ${pageNumber}; ` +
            "refusing to save a possibly partial file."
        );
      }
    }

    const records = [...recordsByValue.values()];

    if (!records.length) {
      throw new Error("No rendered records were collected.");
    }

    await new ObjectsToCsv(records).toDisk(outputPath);
    console.log(`Wrote ${records.length} unique rows to ${outputPath}`);
  } finally {
    await browser.close();
  }
}

main().catch((error) => {
  console.error(`Rendered scrape failed: ${error.message}`);
  process.exitCode = 1;
});

For infinite scroll, replace the Next click with a scroll action and compare the row count or row signature after each batch. Stop only after the count and scroll height remain unchanged for multiple checks, and keep a maximum-pass safety cap.

Stop safely and deduplicate records

Stop when Next is absent or disabled, and keep a page limit. If the control remains enabled but the row signature does not change, report partial pagination rather than saving silently.

The example removes exact duplicates with a Map. Prefer a stable ID or canonical URL when available, since whole-record keys can merge legitimate repeated rows. A guide to web scraping with JavaScript and Node.js can help separate navigation, extraction, and storage.

Troubleshoot common table-scraping failures

When you scrape HTML table in JavaScript, classify failures by retrieval, selection, pagination, or export.

Symptom

Practical fix

Axios parses zero rows

Search the saved response for a known value; if absent, inspect network requests or use a browser

No table matches

Scope a stable ID or container, and include the failed selector in the error

Columns shift

Inspect direct cells and add custom handling for spanning or nested structures

Only page one is saved

Wait for a changed row signature, log page progress, and error on a stalled control

Cell text is blank

Read the relevant link, input, or data attribute instead of text alone

Requests return 403 or 429

Stop, review access, reduce concurrency, and use capped backoff where appropriate

CSV columns break

Use a tested serializer and fixtures containing quotes, commas, newlines, and UTF-8 text

Add responsible production safeguards

If you scrape HTML table in JavaScript beyond a one-off test, use a concise production checklist:

  • Review terms, access rules, and robots guidance before collection. Obtain appropriate review for sensitive or regulated use cases.
  • Keep concurrency conservative, cache unchanged pages, honor Retry-After where applicable, and cap retries with backoff. No delay is universally safe.
  • Collect only necessary fields, restrict access, protect retained data, and delete it when the retention period ends.
  • Log status, selector, page number, row count, and warnings, while excluding credentials and sensitive values.

Legal outcomes can vary by site, data, method, and jurisdiction. Treat this as general engineering guidance, not legal advice. A web scraping without getting blocked playbook can add operational checks after permission and scope are settled.

Key Takeaways

  • The fastest way to scrape HTML table in JavaScript is to inspect the response first, then use a browser only when scripts or interaction are genuinely required.
  • Derive unique keys from headings, generate explicit fallbacks, and flag cell-count mismatches instead of hard-coding one site's columns.
  • Preserve raw strings until conversion rules are known, then validate required columns, nonempty output, warnings, and CSV edge cases.
  • For rendered pagination, wait for a measurable row change, stop on explicit signals, cap the loop, and deduplicate with a meaningful stable key.

FAQ

Why can DevTools show table rows that Axios and Cheerio cannot find?

DevTools shows the live DOM after the browser has executed scripts and normalized markup, while Axios receives the server's response bytes. Client-side code may fetch data, insert rows, or replace a placeholder after load. Browsers can also insert structural elements such as <tbody>, so compare a representative value and account for DOM normalization when testing selectors.

Do I need Puppeteer when a table displays pagination controls?

No. Pagination controls may hide rows already present in the DOM, switch slices of an in-memory array, or request another page from a data endpoint. Use a browser only when changing pages requires JavaScript interaction and no appropriate endpoint is available. Inspect source, DOM row counts, and network responses before adding browser automation.

Select the element inside the cell and read the relevant property. With Cheerio, use .attr("href") for links, .attr("data-id") for data attributes, and .val() for form controls. Resolve relative links with new URL(relativeHref, pageUrl).href. Store visible text and the attribute in separate fields when both carry useful information.

Can I save the parsed records as JSON or send them to a database instead of CSV?

Yes. Write JSON with JSON.stringify(records, null, 2), send records to an API, or insert them into a database. Validate the same schema first. For databases, use parameterized operations, batches or transactions where appropriate, and a stable unique key so reruns can upsert records instead of creating accidental duplicates.

Conclusion

A reliable way to scrape HTML table in JavaScript starts with diagnosis, not a library choice. Confirm whether the rows exist in the response, arrive through a network request, or need a rendered browser. For static markup, Axios and Cheerio provide a lightweight path, while a header-driven parser makes the result reusable across conventional tables rather than one fixed column layout.

Before export, normalize only presentation whitespace, preserve ambiguous values as strings, verify required fields, inspect mismatch warnings, and test CSV quoting with hostile fixtures. For dynamic tables, wait for selectors and row changes, use explicit stop conditions, record pagination progress, and deduplicate with a key that reflects the data. These checks are what separate a demo from a scraper you can debug.

Start with the static script and add complexity only when the page proves it is necessary. If request-layer blocks, CAPTCHA challenges, or proxy rotation become the bottleneck, WebScrapingAPI's Scraper API can return raw HTML while handling those concerns, so you can keep the same Cheerio parsing and validation code.

About the Author

Mihai Maxim, Full Stack Developer @ WebScrapingAPI

Mihai Maxim

Full Stack Developer

Mihai Maxim is a Full Stack Developer at WebScrapingAPI, contributing across the product and helping build reliable tools and features for the platform.

Start Building

Ready to Scale Your Data Collection?

Join 2,000+ companies using WebScrapingAPI to extract web data at enterprise scale with zero infrastructure overhead.