The following code is used to replace all the <img>
tags src
value. But the following code does not modify the original document. $.html
prints the original document and not the modified one.
$ = cheerio.load(data);
$("img").each(function() {
var old_src=$(this).attr("src");
var new_src = "/my_cached_image?url=" + encodeURIComponent(old_src);
$(this).prop("src", new_src);
});
modified_data = $.html();
Use
.attr('src', new_src)
instead of.prop()
.You have a very small error, "src" in an img it's an attribute and not a property.
So this code will work:
output is