我如何获取多个 RSS 提要并将它们合并为一个提要,然后将合并的提要转换为 JSON 数据? (JavaScript)
How can i fetch multiple RSS feeds and merge them in one feed, then convert the merged feed to JSON data? (JavaScript)
我正在尝试将多个 RSS 提要合并为一个并将其转换为 JSON。为了合并 RSS 提要,我使用了这个包:rss-combiner
这是我用来合并 RSS 提要的代码:
var RSSCombiner = require('rss-combiner');
var feedConfig = {
title: 'Tech news from Guardian and BBC',
size: 20,
feeds: [
'http://feeds.bbci.co.uk/news/technology/rss.xml',
'https://www.theguardian.com/uk/technology/rss'
],
pubDate: new Date()
};
RSSCombiner(feedConfig)
.then(function (combinedFeed) {
xml = combinedFeed.xml();
console.log(xml);
});
然后将提要转换为 JSON 我使用了这个包:rss-to-json 和这段代码,它有效:
var Feed = require('rss-to-json');
Feed.load('https://codek.tv/feed/', function(err, rss){
console.log(rss);
});
问题是当我尝试将合并的 Feed 转换为 JSON 时,使用此代码我没有得到任何结果:
var RSSCombiner = require('rss-combiner');
var Feed = require('rss-to-json');
var feedConfig = {
title: 'Tech news from Guardian and BBC',
size: 20,
feeds: [
'http://feeds.bbci.co.uk/news/technology/rss.xml',
'https://www.theguardian.com/uk/technology/rss'
],
pubDate: new Date()
};
// combine feeds
RSSCombiner(feedConfig)
.then(function (combinedFeed) {
var xml = combinedFeed.xml();
// convert combined feed to json
Feed.load(xml, function(err, rss){
console.log(rss);
});
});
我认为 rss-to-json
要求 URL;您传递的是 XML 结果。为什么不在合并 RSS 后直接使用 xml2js
?
const RSSCombiner = require('rss-combiner');
const xml2js = require('xml2js');
const feedConfig = {
title: 'Tech news from Guardian and BBC',
size: 20,
feeds: [
'http://feeds.bbci.co.uk/news/technology/rss.xml',
'https://www.theguardian.com/uk/technology/rss',
],
pubDate: new Date(),
};
RSSCombiner(feedConfig)
.then((combinedFeed) => {
const xml = combinedFeed.xml();
const parser = xml2js.Parser();
parser.parseString(xml, (err, result) => {
console.log(result);
});
});
我正在尝试将多个 RSS 提要合并为一个并将其转换为 JSON。为了合并 RSS 提要,我使用了这个包:rss-combiner
这是我用来合并 RSS 提要的代码:
var RSSCombiner = require('rss-combiner');
var feedConfig = {
title: 'Tech news from Guardian and BBC',
size: 20,
feeds: [
'http://feeds.bbci.co.uk/news/technology/rss.xml',
'https://www.theguardian.com/uk/technology/rss'
],
pubDate: new Date()
};
RSSCombiner(feedConfig)
.then(function (combinedFeed) {
xml = combinedFeed.xml();
console.log(xml);
});
然后将提要转换为 JSON 我使用了这个包:rss-to-json 和这段代码,它有效:
var Feed = require('rss-to-json');
Feed.load('https://codek.tv/feed/', function(err, rss){
console.log(rss);
});
问题是当我尝试将合并的 Feed 转换为 JSON 时,使用此代码我没有得到任何结果:
var RSSCombiner = require('rss-combiner');
var Feed = require('rss-to-json');
var feedConfig = {
title: 'Tech news from Guardian and BBC',
size: 20,
feeds: [
'http://feeds.bbci.co.uk/news/technology/rss.xml',
'https://www.theguardian.com/uk/technology/rss'
],
pubDate: new Date()
};
// combine feeds
RSSCombiner(feedConfig)
.then(function (combinedFeed) {
var xml = combinedFeed.xml();
// convert combined feed to json
Feed.load(xml, function(err, rss){
console.log(rss);
});
});
我认为 rss-to-json
要求 URL;您传递的是 XML 结果。为什么不在合并 RSS 后直接使用 xml2js
?
const RSSCombiner = require('rss-combiner');
const xml2js = require('xml2js');
const feedConfig = {
title: 'Tech news from Guardian and BBC',
size: 20,
feeds: [
'http://feeds.bbci.co.uk/news/technology/rss.xml',
'https://www.theguardian.com/uk/technology/rss',
],
pubDate: new Date(),
};
RSSCombiner(feedConfig)
.then((combinedFeed) => {
const xml = combinedFeed.xml();
const parser = xml2js.Parser();
parser.parseString(xml, (err, result) => {
console.log(result);
});
});