tabletojson中文文档|tabletojson js中文教程|解析
表到 JSON
尝试将 HTML 表转换为 JSON。
可以将单个表的标记作为字符串、HTML 片段或整个页面或只是一个 URL(带有可选的回调函数;也支持承诺)传递。
响应始终是一个数组。响应中的每个数组条目都代表在页面上找到的一个表格(与它们在 HTML 中找到的顺序相同)。
从 2.0 版开始,tabletojson 完全是用打字稿编写的。
!!!注意!!!:自 request.js 被弃用以来,版本 2.0.0 中不兼容的 API 更改。更多信息在这里…
从版本 1.+ 到 2.x 的转换
- 要求必须从更改
const tabletojson = require('../lib/tabletojson');
为
const tabletojson = require('../lib/tabletojson').Tabletojson;
或
const {Tabletojson: tabletojson} = require('../lib/tabletojson');
- 用得到的选项替换请求选项。更多信息在这里…
基本用法
通过 npm 安装
npm install tabletojson
convertUrl
)
远程 ( 'use strict';
const tabletojson = require('tabletojson').Tabletojson;
tabletojson.convertUrl(
'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes',
function(tablesAsJson) {
console.log(tablesAsJson[1]);
}
);
convert
)
本地 ( 看看例子。
// example-6.js
'use strict';
const {Tabletojson: tabletojson} = require('../dist');
const fs = require('fs');
const path = require('path');
const html = fs.readFileSync(path.resolve(__dirname, '../test/tables.html'), {encoding: 'UTF-8'});
const converted = tabletojson.convert(html);
console.log(converted);
重复的列标题
如果有重复的列标题,则后续标题会以计数为后缀:
地方 | 价值 | 地方 | 价值 |
---|---|---|---|
美国广播公司 | 1 | 定义 | 2 |
[{
PLACE: 'abc', VALUE: '1',
PLACE_2: 'def', VALUE_2: '2',
}]
具有行跨度的表
具有 rowspan 的表格,生成的单元格的内容必须在相应的对象中可用。
家长 | 孩子 | 年龄 |
---|---|---|
结婚 | 起诉 | 15 |
史蒂夫 | 12 | |
汤姆 | 3 |
[{
PARENT: 'Marry', CHILD: 'Tom', AGE, '3',
PARENT: 'Marry', CHILD: 'Steve', AGE, '12',
PARENT: 'Marry', CHILD: 'Sue', AGE, '15'
}]
具有复杂行跨度的表
对于具有复杂行跨度的表格,生成的单元格的内容必须在相应的对象中可用。
家长 | 孩子 | 年龄 |
---|---|---|
结婚 | 起诉 | 15 |
史蒂夫 | 12 | |
汤姆 | 3 | |
泰勒 | ||
彼得 | 17 |
[{
PARENT: 'Marry', CHILD: 'Sue', AGE, '15'
PARENT: 'Marry', CHILD: 'Steve', AGE, '12',
PARENT: 'Marry', CHILD: 'Tom', AGE, '3',
PARENT: 'Taylor', CHILD: 'Tom', AGE, '3',
PARENT: 'Taylor', CHILD: 'Peter', AGE, '17'
}]
第一列带有标题的表格
如果表格在第一列中包含标题,您可能会得到意想不到的结果,但您可以传递带有选项的第二个参数,
{ useFirstRowForHeadings: true }
让它像对待任何其他单元格一样对待第一列。
tabletojson.convertUrl(
'https://www.timeanddate.com/holidays/ireland/2017',
{ useFirstRowForHeadings: true },
function(tablesAsJson) {
console.log(tablesAsJson);
}
);
带有 HTML 的表格
默认情况下,以下选项为 true,它将所有值转换为纯文本,为您提供一个更容易阅读的对象:
- 条带HtmlFromHeadings
- 条带HtmlFromCells
如果您的表包含HTML要分析(例如用于链接),你可以设置stripHtmlFromCells
到false
把它作为原始文本。
tabletojson.convertUrl(
'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes',
{ stripHtmlFromCells: false },
function(tablesAsJson) {
//Print out the 1st row from the 2nd table on the above webpage as JSON
console.log(tablesAsJson[1][0]);
}
);
注意:这不适用于嵌套表,它仍会尝试解析。
您可能不需要设置stripHtmlFromHeadings
为false
(并将其设置为 false 会使结果难以解析),但是如果您这样做,您也可以通过设置stripHtml
为false
.
选项
!!!注意力 !!!由于没有主动支持请求,我们需要切换到可靠的请求替换,我决定使用 got。
这是版本 2++ 中不兼容的更改,因此请记住这一点并遵循 Sindre 的迁移指南。
对于使用代理等特殊功能,您应该遵循以下说明:
代理
convertUrl
)
得到(仅我们正在使用 got 来获取远程 HTML 页面。因此,如果您需要从远程服务器获取数据,您可以tabletojson.convertUrl
通过将 got 对象添加到传递给convertUrl
. 有关如何配置请求的更多信息,请查看
代理
tabletojson.convertUrl('https://www.timeanddate.com/holidays/ireland/2017', {
useFirstRowForHeadings: true,
got: {
agent: tunnel.httpOverHttp({
proxy: {
host: 'proxy:8080'
}
})
}
});
条带HtmlFromHeadings
从标题单元格中去除任何 HTML。默认为真。
KEY | <b>VALUE</b>
----|-------------
abc | 1
dev | 2
// Example output with stripHtmlFromHeadings:true
[
{
KEY: 'abc', VALUE: '1'
},
{
KEY: 'dev', VALUE: '2'
}
]
// Example output with stripHtmlFromHeadings:false
[
{
KEY: 'abc', '<b>VALUE</b>': '1'
},
{
KEY: 'dev', '<b>VALUE</b>': '2'
}
]
条带HtmlFromCells
从 tableBody 单元格中去除任何 HTML。默认为真。
KEY | VALUE
----|---------
abc | <i>1</i>
dev | <i>2</i>
// Example output with stripHtmlFromHeadings:true
[
{
KEY: 'abc', VALUE: '1'
},
{
KEY: 'dev', VALUE: '2'
}
]
// Example output with stripHtmlFromHeadings:false
[
{
KEY: 'abc', 'VALUE': '<i>1</i>'
},
{
KEY: 'dev', 'VALUE': '<i>2</i>'
}
]
forceIndexAsNumber
不要使用列文本(有时对数据重新排序),而是将索引强制为数字(字符串编号)。
// Some JSON (Other rows)
{
"0": "",
"1": "A会",
"2": "B会",
"3": "C会",
"4": "Something",
"5": "Else",
"6": ""
}
// Some JSON (Other rows)
计数重复标题
默认为true
。如果设置为false
,重复的标题将不会获得尾随数字。该字段的值将是在表行中找到的最后一个值:
地方 | 价值 | 地方 | 价值 |
---|---|---|---|
美国广播公司 | 1 | 定义 | 2 |
吉 | 3 | jkl | 4 |
// Example output with countDuplicateHeadings:false
[
{
PLACE: 'def', VALUE: '2'
},
{
PLACE: 'jkl', VALUE: '4'
}
]
忽略列
要忽略的索引数组,从 0 开始。默认值为“空/未定义”。
名称 | 地方 | 重量 | 性别 | 年龄 |
---|---|---|---|---|
梅尔 | 1 | 58 | 宽 | 23 |
汤姆 | 2 | 78 | 米 | 54 |
账单 | 3 | 92 | 米 | 31 |
// Example output with ignoreColumns: [2, 3]
[
{
NAME: 'Mel', PLACE: '1', AGE: '23'
},
{
NAME: 'Tom', PLACE: '2', AGE: '54'
},
{
NAME: 'Bill', PLACE: '3', AGE: '31'
}
]
只有列
所采用的索引数组,从 0 开始。默认值为“空/未定义”。如果给定,此选项将覆盖 ignoreColumns。
名称 | 地方 | 重量 | 性别 | 年龄 |
---|---|---|---|---|
梅尔 | 1 | 58 | 宽 | 23 |
汤姆 | 2 | 78 | 米 | 54 |
账单 | 3 | 92 | 米 | 31 |
// Example output with onlyColumns: [0, 4]
[
{
NAME: 'Mel', AGE: '23'
},
{
NAME: 'Tom', AGE: '54'
},
{
NAME: 'Bill', AGE: '31'
}
]
忽略隐藏行
指示是否忽略隐藏行 (display:none)。默认为真:
名称 | 地方 | 重量 | 性别 | 年龄 |
---|---|---|---|---|
梅尔 | 1 | 58 | 宽 | 23 |
汤姆 | 2 | 78 | 米 | 54 |
账单 | 3 | 92 | 米 | 31 |
- 猫 | 4 | 4 | W | 2*
// Example output with ignoreHiddenRows:true
[
{
NAME: 'Mel', PLACE: '1', WEIGHT: '58', SEX: 'W', AGE: '23'
},
{
NAME: 'Tom', PLACE: '2', WEIGHT: '78', SEX: 'M', AGE: '54'
},
{
NAME: 'Bill', PLACE: '3', WEIGHT: '92', SEX: 'M', AGE: '31'
}
]
// Example output with ignoreHiddenRows:false
[
{
NAME: 'Mel', PLACE: '1', WEIGHT: '58', SEX: 'W', AGE: '23'
},
{
NAME: 'Tom', PLACE: '2', WEIGHT: '78', SEX: 'M', AGE: '54'
},
{
NAME: 'Bill', PLACE: '3', WEIGHT: '92', SEX: 'M', AGE: '31'
}
},
{
NAME: 'Cat', PLACE: '4', WEIGHT: '4', SEX: 'W', AGE: '2'
}
]
标题
用作标题的字符串数组。默认为null
/ undefined
。
如果给出的标题多于存在的列,则多计的标题将被忽略。如果给出的标题少于现有值,则忽略多计值。
名称 | 地方 | 重量 | 性别 | 年龄 |
---|---|---|---|---|
梅尔 | 1 | 58 | 宽 | 23 |
汤姆 | 2 | 78 | 米 | 54 |
账单 | 3 | 92 | 米 | 31 |
- 猫 | 4 | 4 | W | 2*
// Example output with headings: ['A','B','C','D','E']
[
{
A: 'Mel', B: '1', C: '58', D: 'W', E: '23'
},
{
A: 'Tom', B: '2', C: '78', D: 'M', E: '54'
},
{
A: 'Bill', B: '3', C: '92', D: 'M', E: '31'
}
]
// Example output with headings: ['A','B','C']
[
{
A: 'Mel', B: '1', C: '58'
},
{
A: 'Tom', B: '2', C: '78'
},
{
A: 'Bill', B: '3', C: '92'
}
]
// Example output with headings: ['A','B','C','D','E','F','G','H']
[
{
A: 'Mel', B: '1', C: '58', D: 'W', E: '23'
},
{
A: 'Tom', B: '2', C: '78', D: 'M', E: '54'
},
{
A: 'Bill', B: '3', C: '92', D: 'M', E: '31'
}
]
// Example output with headings: ['A','B','C'] && ignoreColumns: [2, 3]
[
{
A: 'Mel', B: 'W', C: '23'
},
{
A: 'Tom', B: 'M', C: '54'
},
{
A: 'Bill', B: 'M', C: '31'
}
]
限制行
结果对象应限制在的行数。默认为
null
/ undefined
。
大表(见 test/tables.html)
角色数 | 名称 | 要说的文字 |
---|---|---|
0 | 雷夫帕金森 | redolor in hendrerit in vulputate ve |
1 | 榛舒尔茨 | usto duo dolores et ea rebum。圣 |
2 | 蒙大拿州德尔加多 | psum dolor 坐 amet。Lorem ipsum dolor |
3 | 黛安·麦克布莱德 | 坐我 olor 坐 amet。逻辑推理 |
4 | 西娜·林奇 | us est Lorem ipsum dol |
5 | 纳吉玛控股 | akimata sanctus est Lorem ipsum dolor 坐 |
6 | 奇奇之家 | 我不知道 |
… | ||
197 | 蒙大拿州德尔加多 | 绝杀与反叛。Stet clita kasd gu a |
198 | 默特尔康利 | 反击。Stet clita kasd gubergren, no sea |
199 | 汉娜·埃利斯 | kimata sanctus est Lorem ipsum dolor si |
带有限制行的示例输出:5
[ { 'Roleplayer Number': '0',
Name: 'Raife Parkinson',
'Text to say': 're dolor in hendrerit in vulputate ve' },
{ 'Roleplayer Number': '1',
Name: 'Hazel Schultz',
'Text to say': 'usto duo dolores et ea rebum. Ste' },
{ 'Roleplayer Number': '2',
Name: 'Montana Delgado',
'Text to say': 'psum dolor sit amet. Lorem ipsum dolor sit ame' },
{ 'Roleplayer Number': '3',
Name: 'Dianne Mcbride',
'Text to say': 'olor sit amet. Lorem ipsum' },
{ 'Roleplayer Number': '4',
Name: 'Xena Lynch',
'Text to say': 'us est Lorem ipsum dol' } ]
包含类
使用此类查找特定表的类数组。默认为null
/
undefined
。
已知问题和限制
该模块仅支持解析具有简单水平<th></th>
标题和相应<td></td>
单元格的基本表格
。
它可以在具有复杂结构(例如嵌套表)或多个标题(例如在 X 和 Y 轴上)的表上给出无用或奇怪的结果。
您需要处理诸如确定要解析哪些表和(在大多数情况下)清理数据之类的事情。您可能希望将其与 json2csv 或 CsvToMarkdownTable 等模块结合使用。
如果您想解析由 id 或 class 标识的特定表,您可能希望将它与像“cheerio”这样的模块一起使用(即,使用cheerio选择它们并将它们的HTML作为字符串传递)。
示例用法
// Convert an HTML blob into an array of all the tables on the page
var tabletojson = require('tabletojson').Tabletojson;
var tablesAsJson = tabletojson.convert(html);
var firstTableAsJson = tablesAsJson[0];
var secondTableAsJson = tablesAsJson[1];
...
// Fetch a URL and parse all it's tables into JSON, using a callback
var tabletojson = require('tabletojson').Tabletojson;
var url = 'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes';
tabletojson.convertUrl(url, function(tablesAsJson) {
var listofSovereignStates = tablesAsJson[0];
});
// Fetch a URL and parse all it's tables into JSON, using promises
var tabletojson = require('tabletojson').Tabletojson;
var url = 'http://en.wikipedia.org/wiki/List_of_countries_by_credit_rating';
tabletojson.convertUrl(url)
.then(function(tablesAsJson) {
var standardAndPoorRatings = tablesAsJson[1];
var fitchRatings = tablesAsJson[2];
});
// Fetch a table from Wikipedia and combine with json2csv to convert to CSV
var tabletojson = require('tabletojson').Tabletojson;
var json2csv = require('json2csv');
var url = 'http://en.wikipedia.org/wiki/List_of_countries_by_credit_rating';
tabletojson.convertUrl(url)
.then(function(tablesAsJson) {
var standardAndPoorCreditRatings = tablesAsJson[1];
json2csv({ data: standardAndPoorCreditRatings,
fields: [ 'Country', 'Outlook']
}, function(err, csv) {
console.log(csv);
/* Example output
"Country","Outlook"
"Abu Dhabi, UAE","Stable"
"Albania","Stable"
"Andorra","Negative"
"Angola","Stable"
"Argentina","Negative"
"Aruba","Stable"
"Australia","Stable"
"Austria","Negative"
"Azerbaijan","Positive"
...
*/
});
});
问题
现在表格需要“格式正确”才能转换。未处理的表中的表。
<thead>
<tr>
<th>Header</th>
<tr>
</thead>
贡献
欢迎其他人创建的更好的编写模块的改进、修复和建议,以及针对它无法处理的特定表的错误报告。
您可以在 test 文件夹中找到基本测试。我实现了使用库的最直接的方法。尽管如此,仍有一些边缘情况需要测试,我想在这里寻求支持。随意在此处 fork 和创建 PR。每一点帮助表示赞赏。
为了获得更深入的了解,您可以使用位于此项目随附的示例文件夹中的 Iain 示例,该示例显示了用法,这将是一个良好的开端。
如果您提交拉取请求,请为您的用例添加一个示例,以便我了解您想要它做什么(因为我想为此编写测试并想了解人们拥有的用例类型) .
谢谢
2018 年 6 月 – 特别感谢图书馆的创始人 Iain Collins (@iaincollins)。如果没有他对网站抓取和掌握cheerio 的调查,这个lib 就不会有现在的位置。此外,我个人想说“谢谢”,感谢您信任我将所有权传递给我。马吕斯 (@maugenst)
额外感谢
- @roryok
- 马克斯蒂恩 (@maxthyen)
- 托尔·雅各布森 (@twjacobsen)
- 迈克尔·凯勒 (@mhkeller)
- Jesús Leganés-Combarro (@piranna)
- 若昂·奥塔维奥·费雷拉·巴博萨 (@joaobarbosa)
用于改进和错误修复。