【问题标题】:Parse mutilple children attributes of element using cheerio in NodeJs在Node Js中使用cheerio解析元素的多个子属性
【发布时间】:2021-12-05 22:02:18
【问题描述】:

我有一个看起来像这样的 svg 文件

<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="300.000000pt" height="226.000000pt" viewBox="0 0 300.000000 226.000000"
 preserveAspectRatio="xMidYMid meet">

<g transform="translate(0.000000,226.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">

<path d="M891 2162 c-95 -33 -418 -606 -616 -1092 -75 -185 -106 -330 -70 -67 -2 3 7 36 19 75 13 
27 67 2 -3 -7 -36 -19 -75z"/>

<path d="M1316 2019 c-33 -39 -8035 -57 -37 -58 -82 -59 -25 0 -55 -2 -66 -4 -11 -2 -58 -8 -104 
-15 -158 -22 -330 -77 -315 -101 10 -17 85 -5 311 51 -12 -15 -27z"/>

<path d="M925 1349 c-4 -6 -5 -12 -2 -15 2 -3 7 2 10 11 7 17 1 20 -8 4z"/>

</g>
</svg>

我想获取每个路径元素的 d 属性并将其组合在一个 svg 路径字符串中(由所有三个路径组成) 我试过这个每次只买第一个

   fs.readFile(path_to_svg,'utf8',(err,data)=>{
        if(err) console.log(err);
        $ = cheerio.load(data,{ xmlMode : true });
        $('svg').children().each(function(i,path){
            console.log($('path').attr('d'))
        });
    });

【问题讨论】:

    标签: node.js cheerio


    【解决方案1】:

    你可以得到那些:

    $('svg path').get().map(path => $(path).attr('d'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-27
      • 2013-08-09
      • 1970-01-01
      • 2022-11-26
      • 2016-08-29
      • 2020-05-05
      • 1970-01-01
      • 2015-12-23
      相关资源
      最近更新 更多