Javascript 如何从RSS获取图像URL?

Javascript 如何从RSS获取图像URL?,javascript,node.js,rss,rss-reader,Javascript,Node.js,Rss,Rss Reader,我一直在尝试解析来自各种新闻RSS URL的图像URL,如 为此,我使用了rss解析器库,但文档和示例包含除媒体内容和图像之外的所有其他项目。有没有合适的方法来解析来自新闻频道各种rss URL的图像。?下面是我的尝试 const Parser = require('rss-parser'); const parser = new Parser({ customFields: { feed: ['author'], item: ['categories'

我一直在尝试解析来自各种新闻RSS URL的图像URL,如

为此,我使用了rss解析器库,但文档和示例包含除媒体内容和图像之外的所有其他项目。有没有合适的方法来解析来自新闻频道各种rss URL的图像。?下面是我的尝试

const Parser = require('rss-parser');
const parser = new Parser({
    customFields: {
        feed: ['author'],
        item: ['categories', 'author'],
    }
});


rssUrlParser: async (url,url_id="",department_id="") => {
        try {
            const result = await parser.parseURL(url);
 const feed = {
                "title": result.title || "",
                "link": result.link || "",
                "author": result.author || "",
                "description": result.description || "",
                "image": (result.image && result.url) || "",
                "url_id": url_id,
                "department_id":department_id
            }; 
            const items = result.items.length ? result.items.map(i => {


                i.author = i.author || "";
                i.image = i.enclosure ? i.enclosure.url : "";
                i.categories = i.categories || [];
                i.enclosure = i.enclosure || {};
                return i;
            }) : [];

            return {
                feed,
                items
            };
        } catch (e) {
            return false;
        }

你可以试试这个
so.xml
就是我从上面的url获得的xml内容

const fs=require('fs'))
常量{transform}=require('camaro')
;(异步函数(){
constxml=fs.readFileSync('so.xml','utf-8')
常量模板={
图像:['//media:content[@media=“image”],“@url”]
}
常量结果=等待转换(xml、模板)
log(JSON.stringify(result,null,4));
})()
输出

{
    "images": [
        "https://static01.nyt.com/images/2019/09/16/world/16yemen2-promo/16yemen2-promo-moth-v2.jpg",
        "https://static01.nyt.com/images/2019/09/16/us/politics/16dc-prexy/16dc-prexy-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/business/16UAW1/16UAW1-moth-v2.jpg",
        "https://static01.nyt.com/images/2019/06/19/science/19xp-hair/19xp-hair-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/us/politics/00Dems-Age-promo/00Dems-Age-01-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/us/politics/16dc-trump1/16dc-trump1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/us/politics/16warren-rally-new1/16warren-rally-new1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/us/politics/16dc-coons1-promo/16dc-coons1-promo-moth-v2.jpg",
        "https://static01.nyt.com/images/2019/09/17/pageoneplus/17readers-kavanaugh/17readers-kavanaugh-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/us/politics/15dc-marja1/00dc-marjah1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/17/world/17usbriefing_amcore/17usbriefing_israel_elections-moth.jpg",
        "https://static01.nyt.com/images/2019/09/15/nyregion/17nytoday-1/13giuliani-sub1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/09/us/politics/16daily1/16daily1-moth-v2.jpg",
        "https://static01.nyt.com/images/2019/09/16/business/16db-newsletter-saudi/merlin_160850271_c07da32b-53d5-4cec-9c29-1887c4f262c8-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/opinion/16Landau1/merlin_160480368_10a849bf-724c-4da6-a5d4-0a3d84d9e5ac-moth.jpg",
        "https://static01.nyt.com/images/2019/09/17/opinion/17shehadeh3/17shehadeh3-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/opinion/16saudiarabia/16saudiarabia-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/opinion/16warzel-illo/16warzel-illo-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/opinion/16krugmanWeb/16krugmanWeb-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/opinion/16Bacevich1/16Bacevich1-moth-v2.jpg",
        "https://static01.nyt.com/images/2019/09/16/opinion/16rallyWeb/16rallyWeb-moth.jpg",
        "https://static01.nyt.com/images/2019/09/12/opinion/12sainato/12sainato-moth.jpg",
        "https://static01.nyt.com/images/2019/09/15/opinion/15Leonhardt/merlin_160698273_634a6005-f3ef-44aa-8ccd-c066ae269432-moth.jpg",
        "https://static01.nyt.com/images/2019/09/15/opinion/15Blow/15Blow-moth.jpg",
        "https://static01.nyt.com/images/2019/09/11/multimedia/11xp-skate6/11xp-skate6-moth-v3.jpg",
        "https://static01.nyt.com/images/2019/09/16/us/politics/16dc-judiciary/16dc-judiciary-moth.jpg",
        "https://static01.nyt.com/images/2019/09/17/books/17Chrisrock/17Chrisrock-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/sports/16russia1/merlin_147710904_598b80f7-f69a-48ce-be03-3a3d3f212269-moth.jpg",
        "https://static01.nyt.com/images/2019/09/17/realestate/17trump-jamaica1/merlin_126207707_09d87e42-6d3a-4946-9797-1026ac691fe4-moth.jpg",
        "https://static01.nyt.com/images/2019/09/15/us/politics/15dc-guantanamo1/15dc-guantanamo1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/17/science/00ILSI4/00ILSI4-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/science/16PURDUE1/16PURDUE1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/world/16Brexit-sub/16Brexit-sub-moth.jpg",
        "https://static01.nyt.com/images/2019/09/09/smarter-living/00wc-flossing/00wc-flossing-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/smarter-living/16sl-phonelife/00sl-phonelife-moth.jpg",
        "https://static01.nyt.com/images/2019/08/12/smarter-living/12sl-wrongthing/16sl-wrongthing-moth.jpg",
        "https://static01.nyt.com/images/2019/09/12/magazine/12atwar-stargazing-2/12atwar-stargazing-2-moth.jpg",
        "https://static01.nyt.com/images/2019/09/17/books/17bookbarry1/17bookbarry1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/10/arts/10lopez-answers1/merlin_160385682_2ceafcda-79e1-44c0-bc0b-b3d6a426faf9-moth.jpg",
        "https://static01.nyt.com/images/2019/09/11/dining/06Kitchen-stew1/06Kitchen-stew1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/11/science/11GERMS-NURSINGHOMES1/merlin_160631244_6b1bdb0c-11fe-496d-8c3b-0e243ac1c889-moth.jpg",
        "https://static01.nyt.com/images/2019/09/10/science/10LASKER1/10LASKER1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/17/science/10SCI-MATTER-SKULLS1/10SCI-MATTER-SKULLS1-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/travel/16italy-transumanza1/16italy-transumanza1-moth-v2.jpg",
        "https://static01.nyt.com/images/2019/09/10/t-magazine/10tmag-pepper-slide-5DNI-copy/10tmag-pepper-slide-5DNI-copy-moth-v2.jpg",
        "https://static01.nyt.com/images/2019/09/16/t-magazine/16tmag-romanandwilliams/16tmag-romanandwilliams-moth.jpg",
        "https://static01.nyt.com/images/2019/09/16/sports/16nfl-learned-top/16nfl-learned-top-moth-v2.jpg"
    ]
}

您的方法是完美的,但我通过对constparser=newparser({customFields:{feed:['author'],//item:,item:['categories','author'],['media:content','media']}进行以下更改来解决问题;现在可以通过媒体[“$”]解析url。url来自循环。但另一个需要注意的问题是,一些rss URL具有不同的图像结构。例如,下面提到的url在附件标记中包含新闻的图像@UJIndia不确定我是否理解其他问题:-/