This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

@alexanderliu/cheerio-table-parser
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

Cheerio Table Parser

Usado para converter tabelas em HTML para JSON

Como usar

Para iniciar você precisará instalar a lib cheerio

yarn add cheerio @pedroentringer/cheerio-table-parser

Feito isso basta iniciar as duas libs e fazer o scrapping

import cheerio from 'cheerio';
import withTableParser from '@pedroentringer/cheerio-table-parser';

const html =
  '<table id="tabela"><thead><tr><th>id</th><th>nome</th><th>Data de Nascimento</th><th>idade</th></tr></thead><tbody><tr><td>1</td><td>Pedro Entringer</td><td>05/10/1997</td><td>22</td></tr></tbody></table>';

const $ = withTableParser(cheerio.load(html));

const table = $('#tabela').parseTable();

O resultado sera:

[
  {
    "id": 1,
    "nome": "Pedro Entringer",
    "dataDeNascimento": "05/10/1997",
    "idade": 22
  }
]

Conversão automatica de tipos

O Script convert para String, Number e Boolean

Tabela sem thead

Se sua tabela não tiver os campos em thead, você pode usar a configuração headerIsFirstLine.

import cheerio from 'cheerio';
import withTableParser from '@pedroentringer/cheerio-table-parser';

const html =
  '<table id="tabela"><tbody><tr><th>id</th><th>nome</th><th>Data de Nascimento</th><th>idade</th></tr><tr><td>1</td><td>Pedro Entringer</td><td>05/10/1997</td><td>22</td></tr></tbody></table>';

const $ = withTableParser(cheerio.load(html));

const table = $('#tabela').parseTable({ headerIsFirstLine: true });

O resultado sera:

[
  {
    "id": 1,
    "nome": "Pedro Entringer",
    "dataDeNascimento": "05/10/1997",
    "idade": 22
  }
]

Header customizado

Se sua tabela não tiver header, você poderá definir manualmente usando a configuração headers.

import cheerio from 'cheerio';
import withTableParser from '@pedroentringer/cheerio-table-parser';

const html =
  '<table id="tabela"><tbody><tr><td>1</td><td>Pedro Entringer</td><td>05/10/1997</td><td>22</td></tr></tbody></table>';

const $ = withTableParser(cheerio.load(html));

const table = $('#tabela').parseTable({ headers: ["id", "nome", "dataDeNascimento", "idade"] });

O resultado sera:

[
  {
    "id": 1,
    "nome": "Pedro Entringer",
    "dataDeNascimento": "05/10/1997",
    "idade": 22
  }
]

Readme

Keywords

none

Package Sidebar

Install

npm i @alexanderliu/cheerio-table-parser

Weekly Downloads

2

Version

2.0.2

License

MIT

Unpacked Size

6.93 kB

Total Files

4

Last publish

Collaborators

  • alexanderliu