Cheerio

1.解析表格table #

1
2
3
4
5
6
<tr>
<td></td>
<td colspan="2">Summa tillgodoräknade poäng:</td>
<td class="credits">10,5</td>
<td></td>
</tr>

如果只是一段tr,cheerio不会识别,需要是一个完整的table才可以,而且table外面好像还有包一层div之类的东西,cheerio才能解析。

2.遍历 #

使用each方法可以return false跳出遍历:

To break out of the each loop early, return with false.

https://cheerio.js.org/

2.1. 使用this #

使用this的话,要使用function函数,不能使用箭头函数。function函数的this指向tr。

$('tbody tr').each(function(i, elem) {
	const id = $(this).data();
	console.log(id);
});

2.2. 使用箭头函数就别用this #

因为this不会指向tr,所以要用回调函数的第二个参数。

$('tbody tr').each((i, elem)=>{
	const id = $(elem).data();
	console.log(id);
});