【发布时间】:2017-08-10 02:09:19
【问题描述】:
这是我的示例 html。
<table class = "one">
<thead>..</thead>
<tbody>
<tr>
<td>"Element 1<td>
</tr>
</tbody>
</table>
<table class = "one">
<thead>..</thead>
<tbody>
<tr>
<td>"Element 2<td>
<td>"Element 3<td>
</tr>
</tbody>
</table>
我正在使用cheerio 来解析表格,但不幸的是,我无法选择特定的表格,因为表格没有任何ID 或属于不同的类。 下面的代码从两个表中提取所有行。我必须将输出与已知元素(例如“element2”)进行比较,以修复可用于识别表的索引。但是有没有更好的方法使用cheerio 来获取页面上的第一个表或只获取页面上的第二个表?
var $ = cheerio.load(sampleHTML);
$('table tr').each(function (i, element) { ..}
【问题讨论】:
-
你试过第n个选择器:
'table:nth-child(2) tr'吗?此示例将从第二个表中选择 tr。更多信息:w3schools.com/jquery/sel_nthchild.asp
标签: javascript dom cheerio