使用 cheerio 获取最后一个 div 元素

Get the last div element using cheerio

你好,我正在实现一个 scraper,它可以抓取电报 public 频道中收到的最新消息。这就是 html 是

的方式

我可以获取 class 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
        });

您可以将选择器传递给 jQuery.children() to select all child div elements, and then call jQuery.last() 以获取集合中的最后一个。

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