0
我使用nodejs
xml解析器sax-js从xml
获取内容。该xml
的结构如下:解析时从xml过滤元素
<item>
<title>Some title</title>
<guid isPermaLink="false">http://example.com</guid>
</item>
<item>
<title>VIDEO: Some title</title>
<guid isPermaLink="false">http://example1.com</guid>
</item>
我想下的所有URL,其title
DONOT与VIDEO
开始。
目前,它给了我所有的网址。
我的代码目前是:
'use strict';
var sax = require('sax-js');
var request = require('request');
var href = 'http://some-xml-url.xml';
var urls = [];
var isTextPending = false;
var saxStream = sax.createStream(true);
saxStream.on('error', function (e) {
console.error(e);
});
saxStream.ontext = function (text) {
if(isTextPending) {
urls.push(text);
isTextPending = false;
}
};
saxStream.on('opentag', function (node) {
if(node.name === 'guid' && node.attributes.isPermaLink === 'false') {
isTextPending = true;
}
});
saxStream.end = function() {
}
request(href).pipe(saxStream);
如果您确定XML结构,可以使用RE来提取URL。否则,您可以[将XML转换为json](https://www.npmjs.com/package/fast-xml-parser),然后遍历'item'并检查'item [i] .title'是否不从''VIDEO'开始,然后将'item [i] .guid'存储在某个数组中。 – 2017-11-30 13:58:12