如何从Node.js中的xml中获取url?

如何从Node.js中的xml中获取url?,node.js,xml,express,web-scraping,Node.js,Xml,Express,Web Scraping,我的最终目标是让我的应用程序显示给定用户500px.com帐户(这是一个摄影网站)上X张最新图片的缩略图。据我所知,该网站没有API,但它确实有一个供个人用户使用的rss提要,即可以输出xml 使用xml2js,我可以将xml解析为js对象,并导航到“description”容器,该容器包含包含我想要的url的html,如下所示(这只是使用rss提要中的第一项进行概念验证): 这会将“!CDATA”标记的整个html内容放入photoLink变量中。我想做的是将html中的imgsrc作为目标,

我的最终目标是让我的应用程序显示给定用户500px.com帐户(这是一个摄影网站)上X张最新图片的缩略图。据我所知,该网站没有API,但它确实有一个供个人用户使用的rss提要,即可以输出xml

使用xml2js,我可以将xml解析为js对象,并导航到“description”容器,该容器包含包含我想要的url的html,如下所示(这只是使用rss提要中的第一项进行概念验证):

这会将“!CDATA”标记的整个html内容放入photoLink变量中。我想做的是将html中的
imgsrc
作为目标,这样我就可以将url作为字符串传递到页面上


我可以设想使用字符串方法查找第一个“img src”标记,然后一直读入到地址的末尾,但是有没有更优雅、更简单的方法可以做到这一点呢?

试试这个:在这个例子中,我找到了所有的图像URL

const transform = require('camaro')
const cheerio = require('cheerio')

const xml = require('fs').readFileSync('feed.xml', 'utf-8')

const template = {
    data: ['//item/description', '.']
}

const result = transform(xml, template)

const links = result.data.map(html => {
    const $ = cheerio.load(html)
    const links = $('img')
    const urls = []
    $(links).each(function(i, link) {
        urls.push($(link).attr('src'))
    })
    return urls
})

console.log(links)
输出:

[ [ 'https://drscdn.500px.org/photo/629350/m%3D900/v2?webp=true&sig=4a9fa5788049efb196917cc3f1a55601af901c7157b59ec86c8aa3378c6ee557' ],
  [ 'https://drscdn.500px.org/photo/625259/m%3D900/v2?webp=true&sig=55eab44535f05625ad25dae3e805b2559c1caeb4c97570d04ee0a77c52c7fb19' ],
  [ 'https://drscdn.500px.org/photo/625253/m%3D900/v2?webp=true&sig=174d1b27e6f87e0a98192cf6ae051301681a51beb7297df9733956d2763af163' ],
  [ 'https://drscdn.500px.org/photo/509064/m%3D900/v2?webp=true&sig=698e56114e1d8b67ad11823390f8456ae723d3a389191c43192718f18213caa8' ],
  [ 'https://drscdn.500px.org/photo/509061/m%3D900/v2?webp=true&sig=2998212f82a1c3428cebb873830a99b908f463474045d4e5ebba3257808685dd' ],
  [ 'https://drscdn.500px.org/photo/509060/m%3D900/v2?webp=true&sig=8082904fe1935c51fc301a0d10529475ee15124d3797f69cbaeac3fd6c5f0dcb' ],
  [ 'https://drscdn.500px.org/photo/509056/m%3D900/v2?webp=true&sig=4b85086a7bf55709e77febb202636b0e09415c8ca3fc3657bfb889ad827b3cab' ] ]

您不需要完整的解析器,只需使用正则表达式即可:

var links = [];
var re    = new RegExp("<img.*?src=['\"](.*?)['\"].*?>", "gmi");
var res;

while(res = re.exec(body)) links.push(res[1]);
var-links=[];
var re=新的RegExp(“,“gmi”);
var-res;
而(res=re.exec(body))links.push(res[1]);
例如:

 var a = '<div class="quote"><div class="quote-profile"><img alt="voyages-sncf.com logo" class="img-responsive img-circle" style="height: 80px" src="/img/app_website/index/logo.jpg"> </div><!--//profile--><img alt="voyages-sncf.com logo" class="img-responsive img-circle" style="height: 80px" src="/img/app_website/index/logo2.jpg" data-attr = "lkjlk"/>'

var links = [];
var re    = new RegExp("<img.*?src=['\"](.*?)['\"].*?>", "gmi");
var res;

while(res = re.exec(a)) links.push(res[1]);
//["/img/app_website/index/logo.jpg", "/img/app_website/index/logo2.jpg"]
var a=''
var-links=[];
var re=新的RegExp(“,“gmi”);
var-res;
而(res=re.exec(a))links.push(res[1]);
//[“/img/app_网站/index/logo.jpg”,“/img/app_网站/index/logo2.jpg”]

真的没什么大不了的。使用XML解析器解析RSS并导航到相关元素以提取HTML文本。使用HTML解析器解析HTML并导航到相关元素以提取属性值。你绝对不应该做的一件事是“使用字符串方法”。因为你已经完成了步骤1(RSS解析),剩下的就是步骤2(HTML解析)。请看(基本上是节点的jQuery)以帮助您实现这一点。谢谢!使用cheerio非常有效。谢谢-这种方法很有效。看起来camaro执行的功能与xml2js相同,但速度更快。@测试测试是的,这是camaro的主要目的。以及转换xml的能力;不仅仅是转换。请不要推荐正则表达式来解析HTML。这已经被驳斥了一百万次。这真是个糟糕的建议。“但它避免了一个完整的解析器”并不是一个理由。HTML解析器比正则表达式复杂得多,因为正则表达式不能解析HTML。
 var a = '<div class="quote"><div class="quote-profile"><img alt="voyages-sncf.com logo" class="img-responsive img-circle" style="height: 80px" src="/img/app_website/index/logo.jpg"> </div><!--//profile--><img alt="voyages-sncf.com logo" class="img-responsive img-circle" style="height: 80px" src="/img/app_website/index/logo2.jpg" data-attr = "lkjlk"/>'

var links = [];
var re    = new RegExp("<img.*?src=['\"](.*?)['\"].*?>", "gmi");
var res;

while(res = re.exec(a)) links.push(res[1]);
//["/img/app_website/index/logo.jpg", "/img/app_website/index/logo2.jpg"]