我正在使用 lua-feeds ( http://code.matthewwild.co.uk/lua-feeds/ )
require "lua-feeds/feeds"
local feed = feeds.open("http://php.net/feed.atom");
for _, entry in ipairs(feed) do
bot.rooms["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9d9c1d9e9cac6c7cfccdbccc7cacc87c8d8d887ccdc" rel="noreferrer noopener nofollow">[email protected]</a>"]:send_message(entry:get_child("title"):get_text().."\n"..entry:get_child("link").attr.href);
end
这是我的代码,正在获取完整的 RSS。我只想获取最后一个标题和链接,我该怎么做?
最佳答案
我不明白@DeadMG的回答是如何被接受的。在我看来,这非常错误。
ipairs
接受一个序列并返回其上的迭代器,因此 ipairs(feed)
是 feed 上的迭代器。在 OP 提供的示例中,ipairs
被调用一次。 生成的迭代器被调用多次。
@DeadMG 的想法可能是只调用迭代器一次。看起来是这样的:
local f, v, i = ipairs(feed)
local _, entry = f(v, i)
如果你喜欢技巧:
local _, _, entry = pcall(ipairs(feed))
但是...你为什么要这么做?您将提要作为表格。您想要该表的第一个条目:
local entry = feed[1]
完成。
关于rss - 如何从 RSS 中获取最后一个标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5368823/