npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

puppeteer-table-parser

v2.2.0

Published

Library to make parsing website tables much easier!

Downloads

338

Readme

🕸 🕷 puppeteer-table-parser

Library to make parsing website tables much easier! When you are using puppeteer for scrapping websites and web application, you will find out that parsing tables consistently is not that easy. This library brings you abstraction between puppeteer and page context.

This library solves the following issues:

  • ✨ Parsing columns by their name.
  • ✨ Respect the defined order of columns.
  • ✨ Appending custom columns with custom data.
  • ✨ Custom sanitization of data in cells.
  • ✨ Group and Aggregate data by your own function.
  • ✨ Merge data from two independent tables into one structure.
  • ✨ Handles invalid HTML structure.
  • ✨ Retrieve results as CSV or array of plain JS objects.
  • ✨ And much more!

Installation

yarn add puppeteer-table-parser
npm install puppeteer-table-parser
// CommonJS
const { tableParser } = require('puppeteer-table-parser')

// ESM / Typescript
import { tableParser } from 'puppeteer-table-parser'

API

interface ParserSettings {
  selector: string; // CSS selector
  allowedColNames: Record<string, string>; // key = input name, value = output name)

  headerRowsSelector?: string | null; // (default: 'thead tr', null ignores table's header selection)
  headerRowsCellSelector?: string; // (default: 'td,th')
  bodyRowsSelector?: string;  // (default: 'tbody tr')
  bodyRowsCellSelector?: string;  // (default: 'td')
  reverseTraversal?: boolean // (default: false)
  temporaryColNames?: string[]; // (default: []) 
  extraCols?: ExtraCol[]; // (default: [])
  withHeader?: boolean; // (default: true)
  csvSeparator?: string; // (default: ';')
  newLine?: string; // (default: '\n')
  rowValidationPolicy?: RowValidationPolicy; // (default: 'NON_EMPTY')
  groupBy?: {
    cols: string[];
    handler?: (rows: string[][], getColumnIndex: GetColumnIndexType) => string[];
  }
  rowValidator: (
    row: string[],
    getColumnIndex: GetColumnIndexType,
    rowIndex: number,
    rows: Readonly<string[][]>,
  ) => boolean;
  rowTransform?: (row: string[], getColumnIndex: GetColumnIndexType) => void;
  asArray?: boolean; // (default: false)
  rowValuesAsArray?: boolean; // (default: false)
  rowValuesAsObject?: boolean; // (default: false)
  colFilter?: (elText: string[], index: number) => string; // (default: (txt: string) => txt.join(' '))
  colParser?: (value: string, formattedIndex: number, getColumnIndex: GetColumnIndexType) => string; // (default: (txt: string) => txt.trim())
  optionalColNames?: string[]; // (default: [])
};

Parsing workflow

  1. Find table(s) by provided CSS selector.
  2. Find associated columns by applying colFilter on their text and verify their count.
  3. Filter rows based on rowValidationPolicy
  4. Add extra columns specified in extraCols property in settings.
  5. Run rowValidator function for every table row.
  6. Run colParser for every cell in a row.
  7. Run rowTransform function for each row.
  8. Group results into buckets (groupBy.cols) property and pick the aggregated rows.
  9. Add processed row to a temp array result.
  10. Add header column if withHeader property is true.
  11. Merge partial results and return them.

Examples

All data came from the HTML page, which you can find in test/assets/1.html.

Basic example (the simple table where we want to parse three columns without editing)

import { tableParser } from 'puppeteer-table-parser'

await tableParser(page, {
  selector: 'table',
  allowedColNames: {
    'Car Name': 'car',
    'Horse Powers': 'hp',
    'Manufacture Year': 'year',
  },
});
car;hp;year
Audi S5;332;2015
Alfa Romeo Giulia;500;2020
BMW X3;215;2017
Skoda Octavia;120;2012

Basic example with custom column name parsing:

import { tableParser } from 'puppeteer-table-parser'

await tableParser(page, {
  selector: 'table',
  colFilter: (value: string[]) => {
    return value.join(' ').replace(' ', '-').toLowerCase();
  },
  colParser: (value: string) => {
    return value.trim();
  },
  allowedColNames: {
    'car-name': 'car',
    'horse-powers': 'hp',
    'manufacture-year': 'year',
  },
})
car;hp;year
Audi S5;332;2015
Alfa Romeo Giulia;500;2020
BMW X3;215;2017
Skoda Octavia;120;2012

Basic example with row validation and using temporary column.

import { tableParser } from 'puppeteer-table-parser'

await tableParser(page, {
  selector: 'table',
  allowedColNames: {
    'Car Name': 'car',
    'Manufacture Year': 'year',
    'Horse Powers': 'hp',
  },
  temporaryColNames: ['Horse Powers'],
  rowValidator: (row: string[], getColumnIndex) => {
    const powerIndex = getColumnIndex('hp');
    return Number(row[powerIndex]) < 250;
  },
});
car;year
BMW X3;2017
Skoda Octavia;2012

Advanced example:

Uses custom temporary column for filtering. It uses an extra column with custom position to be filled on a fly.

import { tableParser } from 'puppeteer-table-parser'

await tableParser(page, {
  selector: 'table',
  allowedColNames: {
    'Manufacture Year': 'year',
    'Horse Powers': 'hp',
    'Car Name': 'car',
  },
  temporaryColNames: ['Horse Powers'],
  extraCols: [
    {
      colName: 'favorite',
      data: '',
      position: 0,
    },
  ],
  rowValidator: (row: string[], getColumnIndex) => {
    const horsePowerIndex = getColumnIndex('hp');
    return Number(row[horsePowerIndex]) > 150;
  },
  rowTransform: (row: string[], getColumnIndex) => {
    const nameIndex = getColumnIndex('car');
    const favoriteIndex = getColumnIndex('favorite');

    if (row[nameIndex].includes('Alfa Romeo')) {
      row[favoriteIndex] = 'YES';
    } else {
      row[favoriteIndex] = 'NO';
    }
  },
  asArray: false,
  rowValuesAsArray: false
});
favorite;year;car
NO;2015;Audi S5
YES;2020;Alfa Romeo Giulia
NO;2017;BMW X3

Optional columns

Sometimes you can be in a situation where some if your columns are desired, but they are not available in a table. You can easily add an exception for them via optionalColNames property.

import { tableParser } from 'puppeteer-table-parser'

await tableParser(page, {
  selector: 'table',
  allowedColNames: {
    'Car Name': 'car',
    'Rating': 'rating',
  },
  optionalColNames: ['rating']
});

Grouping and Aggregating

import { tableParser } from 'puppeteer-table-parser'

await tableParser(page, {
  selector: '#my-table',
  allowedColNames: {
    'Employee Name': 'name',
    'Age': 'age',
  },
  groupBy: {
    cols: ['name'],
    handler: (rows: string[][], getColumnIndex) => {
      const ageIndex = getColumnIndex('age');

      // select one with the minimal age
      return rows.reduce((previous, current) =>
        previous[ageIndex] < current[ageIndex] ? previous : current,
      );
    },
  }
});

For more, look at the test folder! 🙈