在html表中的Node.js Cheerio Parse html表

时间:2017-05-31 03:57:47

标签: javascript html node.js parsing cheerio

<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);
    });    

1 个答案:

答案 0 :(得分: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文本值。希望这可以帮助别人。