我想使用 Ruby 的RSS 类来解析 Atom 和 RSS 提要,所以我可以从中提取链接。如何区分代码中的两种类型?
我已经准备好了解析器响应。
response = RSS::Parser.parse(rss_url, false)
我找到了.feed_type
feed 对象的方法,并像这样使用它:
if response.feed_type == "rss"
puts "hey rss"
response.channel.items.each{ |item| links += "'#{item.link}'," }
elsif response.feed_type == "atom"
puts "hey atom"
response.entries.each{ |entry| links += "'#{entry.link.href}'," }
else
puts "something went wrong"
end