【问题标题】:Cheerio find all links in nested tableCheerio 在嵌套表中查找所有链接
【发布时间】:2021-06-11 03:53:18
【问题描述】:

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

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>

【问题讨论】:

    标签: javascript jquery node.js web-scraping cheerio


    【解决方案1】:

    看起来你的选择器有问题,试试看:

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

    【讨论】:

    • 感谢您的回复,很遗憾,没有任何输出。
    • 新链接工作正常!我认为它在真实网站上不起作用,因为 TableWithRules div 在另一个 div 中,但现在我明白了它是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多