前端集合 - 关注前端技术和互联网免费资源

关注前端技术和互联网免费资源

分类儿

页面儿

搜索儿

前端集合 RSS订阅
Home » 前端集合 » 6大Node.js RSS Feed解析器

6大Node.js RSS Feed解析器

发布者:前端集合 // 发布时间:2016-12-27 20:47:00 // 分类:前端集合 // 访问: 10,301 次 // 热度:

10大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');
});

Tags: node.js, node, node rss, node feed, node rss parser, node feed parser, node xml parser, node xml, node rss 解析器, node feed 解析器

2016,变化太多>>  << 如何解决WebStorm can't start git.exe的错误?
Top