【问题标题】:Table to JSON using node.js使用 node.js 将表转换为 JSON
【发布时间】:2019-02-20 10:21:59
【问题描述】:

我正在尝试将表格转换为 JSON,以便轻松搜索数据,网址为:http://www.tppcrpg.net/rarity.html

我找到了这个包: https://www.npmjs.com/package/tabletojson

我试着像这样使用它:

'use strict';

const tabletojson = require('tabletojson');

        tabletojson.convertUrl(
            'http://www.tppcrpg.net/rarity.html',
            { useFirstRowForHeadings: true },
            function(tablesAsJson) {
                console.log(tablesAsJson[1]);
            }
        );

但是它在控制台中返回 undefined,是否有任何替代选项或者我使用的包错误?

【问题讨论】:

  • 你想转换这个远程网址吗?
  • 是的,我想以 JSON 格式获取数据,因为我不知道如何从表格格式的行中获取数据
  • tppcrg url 是你的吗?
  • 不,这不是我的
  • 好的,您无法访问第三方 api 的数据,但是您想要该数据,所以您正在尝试这样做对吗?

标签: html json node.js


【解决方案1】:

嘿,你实际上是在获取数据,更改 console.log

您的输出总共只有一个数组,但您将 tablesAsJson[1] 放在控制台中,但数组索引以 [0] 开头。

'use strict';

    const tabletojson = require('tabletojson');
    tabletojson.convertUrl(
        'http://www.tppcrpg.net/rarity.html',
        function(tablesAsJson) {
            console.log(tablesAsJson[0]);
        }
    );

为了更好看的代码:

const url = 'http://www.tppcrpg.net/rarity.html';
tabletojson.convertUrl(url)
  .then((data) => {
    console.log(data[0]);
  })
  .catch((err) => { 
     console.log('err', err);
   }); // to catch error

【讨论】:

猜你喜欢
  • 2018-02-08
  • 2019-03-19
  • 2013-07-06
  • 1970-01-01
  • 2022-11-17
  • 2015-02-07
  • 1970-01-01
  • 2017-10-08
  • 2016-11-09
相关资源
最近更新 更多