javascript - Cheerio 在嵌套表中查找所有链接
问题描述
我正在尝试检索特定表中的所有链接,但选择器似乎不起作用。当我将输出记录到控制台时,什么也没有出现,我做错了什么?
节点代码:
$ = cheerio.load(body);
$("#TableWithRules").find("a").each(function(i, link){
if(i == 10) return;
console.log($(link).text() , $(link).attr('href'), true);
});
HTML 代码:
<div id="TableWithRules">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td valign="top" nowrap="nowrap"><a GET THIS LINK ==> href="/cgi-bin/cvename.cgi?name=CVE-2020-5911">CVE-2020-5911</a></td>
<td valign="top">In versions 3.0.0-3.5.0, 2.0.0-2.9.0, and 1.0.1, the NGINX Controller installer starts the download of Kubernetes packages from an HTTP URL On Debian/Ubuntu system.
</td>
</tr>
<tr>
<td valign="top" nowrap="nowrap"><a GET THIS LINK ==> href="/cgi-bin/cvename.cgi?name=CVE-2020-27730">CVE-2020-27730</a></td>
<td valign="top">In versions 3.0.0-3.9.0, 2.0.0-2.9.0, and 1.0.1, the NGINX Controller Agent does not use absolute paths when calling system utilities.
</td>
</tr>
</table>
</div>
解决方案
看起来您的选择器错误,请尝试:
$("#TableWithRules > table > tbody > tr > td").find("a").each(function(i, link){
console.log($(link).text() , $(link).attr('href'), true);
});
推荐阅读
- python - 我们可以在 react 应用程序中使用 plotly dash 应用程序吗?
- java - Gstreamer 在 JavaForm 上运行
- reactjs - React:更新 Firebase 适用于输入,不适用于选择
- reactjs - 如何在将对象作为道具传递时测试组件?
- node.js - 快速查询字符串检查并发送给客户端
- javascript - 如何检测使用 javascript 关闭整个浏览器?
- ios - 当您的数据源是 Results 对象时,如何在表格视图中移动行?
- jenkins - 从 ACR 拉取时,ImagePullBackOff 带有“rpc 错误:代码 = 未知 desc = 无法从 AKS 拉取和解压缩图像”
- flutter - 使用列表中的地图转换列表列表
- django - 如何列出组中的所有频道名称?