javascript - Cheerio 查找嵌套表中的所有链接

标签 javascript jquery node.js web-scraping cheerio

我正在尝试检索特定表中的所有链接,但选择器似乎不起作用。当我将输出记录到控制台时,什么也没有出现,我做错了什么?

NodeJS 代码:

  $ = 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);
});

关于javascript - Cheerio 查找嵌套表中的所有链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66611606/

相关文章:

c# - 部分 View 上的 mvc4 分页

javascript - Drupal 7 Form api 使用 Jquery 自定义 javascript

javascript - 这两种传递url参数的方式有什么区别 "/blog/12"和 "/blog?id=12"

javascript - 我应该在哪里放置与 redux 中的 Action 相关的同步副作用?

javascript - 如何按属性名称的顺序控制台记录整个 HTML DOM 树

javascript - 为什么我的关键帧动画不能在我的 div 上运行?

javascript - Onbeforeunload 不适用于 Chrome/Firefox,但适用于 IE

javascript - 只返回数组中没有的随机数

css - 如何使用 `less` 中的 `express-js` 缩小 CSS?

javascript - 有没有一种方法可以在不先编译的情况下渲染字符串灰尘模板?