首页 > 解决方案 > 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>

标签: javascriptjquerynode.jsweb-scrapingcheerio

解决方案


看起来您的选择器错误,请尝试:

$("#TableWithRules > table > tbody > tr > td").find("a").each(function(i, link){
    console.log($(link).text() , $(link).attr('href'), true);
});

推荐阅读