在Cheerio.js的表中迭代TR

问题描述 投票:6回答:1

我在使用我在节点服务器上使用的Cheerio.js选择器时遇到问题。据说核心基于jQuery,但是我无法使用与原生jQuery相同的选择来使其工作。

我有一个大致如下的DOM:

<div class="test">
    <table class="listing">
        <thead><tr>few cells here</tr></thead>
        <tfoot></tfoot>
        <tbody><tr>These are the rows I want</tr></tbody>
    </table>
</div>

由于页面上有两个带有“列表”类的表,我不能直接选择它,所以我需要使用“test”类来引用div。我可以使用jQuery运行的选择类似于:

$('div.test tbody tr')

但这不适用于Cheerio。如果我运行$('div [class =“test”] tr')我得到表上的所有行,甚至是thead行,所以这对我不起作用。

任何猜测?

更新:这是我正在执行的实际代码(不起作用):

// Load the html
var $ = cheerio.load(html, {
    normalizeWhitespace: true
});

$('div.tillgodo tbody tr').each(function(){
    console.log("Found credited course...");
    var children = $(this).children();
    var credits = parseFloat($(children[3]).text().replace(',', '.')); // We need to replace comma with a dot since parseFloats only supports dots by design

    var row = {
        "course" : $(children[1]).text().trim(),
        "grade" : null,
        "credits" : credits,
        "date" : $(children[4]).text()
    };

    // Push course to JSON object
    console.log("Push course to object...");
    console.log("------------------------------------------\n");
    data.credited_courses.push(row);
    data.credited_courses_credits += parseFloat(credits);
});

以下代码适用于第一个表:

$('tr.incomplete.course').each(function(i, tr){
    console.log("This is course nr: " + parseInt(course_count+1));
    console.log("Found incompleted course...");
    var children = $(this).children();
    var credits = parseFloat($(children[2]).text().replace(',', '.').match(/(\+|-)?((\d+(\.\d+)?)|(\.\d+))/)[0]); // Filter out any parentheses and odd characters
    var row = {
        "course" : $(children[1]).text(),
        "grade" : $(children[3]).text(),
        "credits" : credits,
        "date" : $(children[5]).text()
    };

    // Sum the total amount of credits for all courses
    console.log("Add credits to incompleted_credits...");
    data.incompleted_credits += credits;

    console.log("Push course to object...");
    data.incompleted_courses.push(row);
    course_count++;
});

当我说它不起作用意味着我正在返回的JSON对象没有来自第二个表的预期行。

更新2我要抓的表:

<div class="tillgodo">
    <h2>Tillgodoräknanden</h2>
    <table class="listing">
    <thead>
    <tr class="listingHeader">
        <th>Kurskod</th>
        <th>Kursnamn</th>
        <th>Beslutsfattare</th>
        <th class="credits">Poäng</th>
        <th>Datum</th>
    </tr>
    </thead>
    <tfoot>
    <tr class="listingTrailer">
        <td>
        </td><td colspan="2">Summa tillgodoräknade poäng:
        </td><td class="credits">10,5
        </td><td>
    </td></tr>
    </tfoot>

        <tbody><tr>
            <td>
            </td><td>Valfria kurser
            </td><td>xxx
            </td><td class="credits">10,5
            </td><td class="nobreak">2013-06-03
        </td></tr>

    </tbody>
</table>
</div>

最后的更新(问题解决了)我一直在使用的选择器正在工作。但源HTML格式错误,根本没有标签。浏览器(在我的情况下是Chrome)修复了问题,但很难找到真正的问题。

jquery node.js cheerio
1个回答
0
投票

你可以试试$(div.test table.listing tr).text()

这将为您提供该表中所有tr标签的文本

© www.soinside.com 2019 - 2024. All rights reserved.