使用 Cheerio 解析 Table
Parsing Table with Cheerio
<html>
<body>
<table>
<tbody>
<tr>
<!-- More things here -->
</tr>
<tr>
<td>
<table></table>
<table>
<tbody>
<tr>
<td></td>
<td>I want this</td>
</tr>
</tbody>
</table>
<table></table>
</td>
</tr>
<tr>
<!-- More things here -->
</tr>
</tbody>
</table>
</body>
</html>
所以这是看起来很奇怪的 HTMl 结构。我想要的元素是我在 HTML.
中标记的 '' 元素
这是我用来解析 HTML 的 Node Js 代码。我不是很熟悉 jQuery.
const $ = cheerio.load(body);
$('table').find('tbody tr:nth-child(1)').each(()=> {
$('td').find('table').each(() => {
console.log($(this).text())
})
})
如果您显示的结构是固定的(行数等)...
$('table tr:nth-child(2) table:nth-child(2) tr td:nth-child(2)').text()
希望对你有帮助
<html>
<body>
<table>
<tbody>
<tr>
<!-- More things here -->
</tr>
<tr>
<td>
<table></table>
<table>
<tbody>
<tr>
<td></td>
<td>I want this</td>
</tr>
</tbody>
</table>
<table></table>
</td>
</tr>
<tr>
<!-- More things here -->
</tr>
</tbody>
</table>
</body>
</html>
所以这是看起来很奇怪的 HTMl 结构。我想要的元素是我在 HTML.
中标记的 '' 元素这是我用来解析 HTML 的 Node Js 代码。我不是很熟悉 jQuery.
const $ = cheerio.load(body);
$('table').find('tbody tr:nth-child(1)').each(()=> {
$('td').find('table').each(() => {
console.log($(this).text())
})
})
如果您显示的结构是固定的(行数等)...
$('table tr:nth-child(2) table:nth-child(2) tr td:nth-child(2)').text()
希望对你有帮助