<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 值。我得到了两张 table ,而且很乱。
$ = 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);
});
最佳答案
$ = 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 文本值。希望这可以帮助其他人。
关于javascript - html 表中的 Node.js Cheerio 解析 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44275369/