cheerio-tableparser
HTML table parser. Most popular cheerio plugin in the world! Port of jquery.tableparser plugin to cheerio.
Installation
npm install --save cheerio cheerio-tableparser
Introduction
Parsing HTML table could be difficult when its structure contains colspan or rowspan.
Cheerio-tableparser parse HTML tables, group them by columns, with colspan and rowspan respected.
var cheerio = require('cheerio'),
cheerioTableparser = require('cheerio-tableparser'),
$ = cheerio.load("<table id='complex'> \
<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr> \
<tr><td rowspan='5'>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr> \
<tr><td rowspan='2' colspan='2'>2b</td><td>4b</td><td>5b</td></tr> \
<tr><td rowspan='2'>4c</td><td>5c</td></tr> \
<tr><td rowspan='2'>2d</td><td>3d</td><td>5d</td></tr> \
<tr><td>3e</td><td>4e</td><td>5e</td></tr> \
</table>");
cheerioTableparser($);
var data = $("#complex").parsetable(false, false, false);
Features
⏎ Easy to use:
You just need cheerio and cheerio-tablerparser to parse complex tables, as easy as JSON.parse(). You get array of columns.
⚙ Configurable:
Cheerio-tableparser can duplicate empty cells in rows or columns or even both. Result can be text or html.
ϟ Fast:
As fast as cheerio is.
When I should use Cheerio-tableparser
Cheerio-tableparser used only for tables created with “table” tag. In other unusual situations you should use other solutions. For example css formated text looks like table is not affordable.
How it works
var cheerio = require('cheerio'),
cheerioTableparser = require('cheerio-tableparser'),
$ = cheerio.load("<table></table>");
cheerioTableparser($);
data = $("table").parsetable();
.parsetable(dupCols, dupRows, textMode)
dupCols
: if true empty cells will be copy of left not empty column. If false empty cell. Default: false.dupRows
: if true empty cells will be copy of upper row. If false empty cell. Default: false.textMode
: if true result will be text same as cell $("td").text(). If false result will be HTML same as cell $("td").html(). Default: false.
Example table:
|-------------------|
| A | B | C | D | E |
|-------------------|
| | 2 | 3 | 4 | 5 |
| |---------------|
| | | 4b| 5b|
| | 2b |-------|
| 1 | | | 5c|
| |-------| 4c|---|
| | | 3d| | 5d|
| | 2d|-----------|
| | | 3e| 4e| 5e|
|-------------------|
Load data and add plugin
var cheerio = require('cheerio'),
cheerioTableparser = require('cheerio-tableparser'),
$ = cheerio.load("<table id='complex'> \
<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr> \
<tr><td rowspan='5'>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr> \
<tr><td rowspan='2' colspan='2'>2b</td><td>4b</td><td>5b</td></tr> \
<tr><td rowspan='2'>4c</td><td>5c</td></tr> \
<tr><td rowspan='2'>2d</td><td>3d</td><td>5d</td></tr> \
<tr><td>3e</td><td>4e</td><td>5e</td></tr> \
</table>");
cheerioTableparser($);
Copy empty cells from data in columns
var data = $("#complex").parsetable(false, true, false);
Copy empty cells from data in rows
var data = $("#complex").parsetable(true, false, false);
Copy empty cells from data in rows AND columns
var data = $("#complex").parsetable(true, true, false);
Default usage
var data = $("#complex").parsetable();
Example HTML table:
|-------------------------------------------------------------------|
| <strong>A</strong> | <strong>B</strong> |
|-------------------------------------------------------------------|
| <div class='table-text'>1</div> | <div class='table-text'>2</div> |
|-------------------------------------------------------------------|
Load data and add plugin
var cheerio = require('cheerio'),
cheerioTableparser = require('cheerio-tableparser'),
$ = cheerio.load("<table id='html'> \
<tr><td><strong>A</strong></td><td><strong>B</strong></td></tr> \
<tr><td><div class='table-text'>1</div></td><td><div class='table-text'>2</div></td></tr> \
</table>");
cheerioTableparser($);
Return data as HTML
var data = $("#html").parsetable(false, false, false);
Return data as Text
var data = $("#html").parsetable(false, false, true);
License
(The MIT License)
Copyright (c) 2016 Mister Parser
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.