6大Node.js RSS Feed解析器
基于Node.js的RSS或Feed解析器还是挺多的,使用简单方便。以下排名分先后:
1. node-feedparser
- 示例
- 安装:
npm install feedparser
- 使用方法:
var FeedParser = require('feedparser');
var request = require('request'); // for fetching the feed
var req = request('http://somefeedurl.xml')
var feedparser = new FeedParser([options]);
req.on('error', function (error) {
// handle any request errors
});
req.on('response', function (res) {
var stream = this; // `this` is `req`, which is a stream
if (res.statusCode !== 200) {
this.emit('error', new Error('Bad status code'));
}
else {
stream.pipe(feedparser);
}
});
feedparser.on('error', function (error) {
// always handle errors
});
feedparser.on('readable', function () {
// This is where the action is!
var stream = this; // `this` is `feedparser`, which is a stream
var meta = this.meta; // **NOTE** the "meta" is always available in the context of the feedparser instance
var item;
while (item = stream.read()) {
console.log(item);
}
});
2. node-rss
- 示例
- 安装
npm install node-rss
3. node-rssparser
- 安装
npm install rssparser
- 使用方法
var parser = require('rssparser');
var options = {};
//rss feeds
parser.parseURL('http://laymansite.com/rss', options, function(err, out){
console.log(out);
});
3. feed-read
- 安装
npm install feed-read
- 使用方法
var feed = require("feed-read");
feed("http://craphound.com/?feed=rss2", function(err, articles) {
if (err) throw err;
// Each article has the following properties:
//
// * "title" - The article title (String).
// * "author" - The author's name (String).
// * "link" - The original article link (String).
// * "content" - The HTML content of the article (String).
// * "published" - The date that the article was published (Date).
// * "feed" - {name, source, link}
//
});
4. feedme
- 安装
npm install feedme
- 使用方法
var FeedMe = require('feedme');
var http = require('http');
http.get('http://www.npr.org/rss/rss.php?id=1001', function(res) {
var parser = new FeedMe();
parser.on('title', function(title) {
console.log('title of feed is', title);
});
parser.on('item', function(item) {
console.log();
console.log('news:', item.title);
console.log(item.description);
});
res.pipe(parser);
});
5. rss-parser
该组件可以在浏览器端使用。
- 安装
npm install rss-parser
- 使用方法
Node.js端:
var parser = require('rss-parser');
parser.parseURL('https://www.reddit.com/.rss', function(err, parsed) {
console.log(parsed.feed.title);
parsed.feed.entries.forEach(function(entry) {
console.log(entry.title + ':' + entry.link);
})
})
浏览器端:
<script src="/bower_components/rss-parser/dist/rss-parser.js"></script>
<script>
RSSParser.parseURL('https://www.reddit.com/.rss', function(err, parsed) {
console.log(parsed.feed.title);
parsed.feed.entries.forEach(function(entry) {
console.log(entry.title + ':' + entry.link);
})
})
</script>
6. feed-reader
- 安装
npm install feed-reader
- 使用方法
普通版示例:
var reader = require('feed-reader');
reader.parse(SOME_RSS_URL);
Promise版示例:
var parse = require('feed-reader').parse;
let url = 'https://news.google.com/news/feeds?pz=1&cf=all&ned=us&hl=en&q=nodejs&output=rss';
parse(url).then((feed) => {
console.log(feed);
}).catch((err) => {
console.log(err);
}).finally(() => {
console.log('Everything done');
});