【问题标题】:Get the last div element using cheerio使用 Cheerio 获取最后一个 div 元素
【发布时间】:2021-06-19 18:17:33
【问题描述】:

您好,我正在实现一个 scraper,它会抓取在电报公共频道中收到的最新消息。这就是html的

我可以获取tgme_channel_history 部分类下的所有 div。它下面有五个 div,这意味着五个不同的消息。我只想得到最后一个 div。我怎样才能做到这一点。 您可以在此处查看实时 HTML https://telegram.me/s/newlink01/

const $ = require('cheerio');

const url = 'https://telegram.me/s/newlink01/';

 rp(url)
        .then(function (html) {
            //success!
            const wikiUrls = [];

            $('.tgme_channel_history', html).each(function () {

                wikiUrls.push($(this).text()


            });
            console.log(wikiUrls[0]);

  })
        .catch(function (err) {
            //handle error
        });

【问题讨论】:

    标签: javascript jquery node.js cheerio


    【解决方案1】:

    您可以将选择器传递给jQuery.children() 以选择所有子div 元素,然后调用jQuery.last() 以获取集合中的最后一个。

    $(".tgme_channel_history").children("div").last();
    

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    相关资源
    最近更新 更多