从html数据中获取并替换img src值

时间:2021-02-03 20:35:15

I have html file and it stored in AWS S3. I already read that html content file with NodeJS AWS-SDK function(getObject) and it works very well then give me a data. The question is, how to get "src" url from that html data? and how to replace it with new url?

我有html文件,它存储在AWS S3中。我已经阅读了带有NodeJS AWS-SDK功能(getObject)的html内容文件,它运行得非常好,然后给我一个数据。问题是,如何从该html数据中获取“src”url?以及如何用新网址替换它?

this my example code, I run it in cmd windows :

这是我的示例代码,我在cmd窗口中运行它:

var AWS = require('aws-sdk');

AWS.config.loadFromPath('./config.json');
var s3 = new AWS.S3();
var params = {Bucket: 'myStoreName/content', Key: 'index.html'};

s3.getObject(params, function(err, data) {
  if (err) {
    console.log(err, err.stack);
  }
  else {
    var html = data.Body.toString();
    console.log(html);
  }    
});

The result from code above is :

上面代码的结果是:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <h1>Hello World</h1>
        <p>This is sample of test</p>
        <img src="./myimage.jpg" />
    </body>
</html>

All I want just replce src url to be src="cid:unique@kreata.ee". Is there anyone know how to solve it? is there other way? thankyou for any help

我只想将src url重新命名为src =“cid:unique@kreata.ee”。有没有人知道如何解决它?还有其他方法吗?感谢您的任何帮助

1 个解决方案

#1


1  

You might want to use some parser for this.

您可能希望使用一些解析器。

Cheerio is my choice.

Cheerio是我的选择。

var AWS = require('aws-sdk');
var cheerio = require('cheerio');

AWS.config.loadFromPath('./config.json');
var s3 = new AWS.S3();
var params = {Bucket: 'myStoreName/content', Key: 'index.html'};

s3.getObject(params, function(err, data) {
  if (err) {
    console.log(err, err.stack);
  }
  else {
    var $ = cheerio.load( data.Body.toString() );
    $('body').find('img').attr('src', 'SRC_VALUE_TO_SET');
        console.log( $.html() );
  }    
});

Hope that helps.

希望有所帮助。

#1


1  

You might want to use some parser for this.

您可能希望使用一些解析器。

Cheerio is my choice.

Cheerio是我的选择。

var AWS = require('aws-sdk');
var cheerio = require('cheerio');

AWS.config.loadFromPath('./config.json');
var s3 = new AWS.S3();
var params = {Bucket: 'myStoreName/content', Key: 'index.html'};

s3.getObject(params, function(err, data) {
  if (err) {
    console.log(err, err.stack);
  }
  else {
    var $ = cheerio.load( data.Body.toString() );
    $('body').find('img').attr('src', 'SRC_VALUE_TO_SET');
        console.log( $.html() );
  }    
});

Hope that helps.

希望有所帮助。