【问题标题】:Node.js Cheerio Parse html table inside a html tableNode.js Cheerio 在一个 html 表中解析 html 表
【发布时间】:2017-10-31 16:53:10
【问题描述】:
<table cellspacing="0" cellpadding="0" width="90%" align="center" border="0">       
        <tr>
            <td>

                        <table cellpadding="0" cellspacing="1" width="90%" border="0" align="center">
                            <tr>
                                <td

整个第二个表在第一个表的 td 标记内。

我是新来的cheerio。我不能完全让我的输出只给我内表 tr 值。我得到了两张桌子,而且很乱。

$ = cheerio.load(html.toString());
var data = [];
    $('tr').each(function(i, tr){

        var children = $(this).children();
        var itemNum = children.eq(0);

        var row = {
            "Num": itemNum.text().trim()
        };
        data.push(row);
        console.log(row);
    });    

【问题讨论】:

  • 任何帮助表示赞赏

标签: javascript html node.js parsing cheerio


【解决方案1】:
   $ = cheerio.load(html.toString());
var data = [];
    $('table tr td table tr').each(function(i, td){

        var children = $(this).children();
        var itemNum = children.eq(0);
        var itemName = children.eq(1);

此代码解决了该问题。我没有意识到你只是传递元素直到你得到你需要的元素然后使用 children.eq(n) 我能够获取行中的每个 td 文本值。希望这可以帮助其他人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2012-01-12
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多