问题描述
您好,我正在实施一个 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
});
解决方法
您可以将选择器传递给 jQuery.children()
以选择所有子 div
元素,然后调用 jQuery.last()
获取集合中的最后一个。
$(".tgme_channel_history").children("div").last();