我正在尝试使用node.js和puppeteer下载图像,但遇到了一些问题。我使用网络爬行器从站点收集图像的链接,然后使用https/http包下载图像。
这适用于使用http和https源的图像,但有些图像的链接看起来像这样(整个链接非常长,所以我删掉了其余部分):
数据:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAw8AAADGCAYAAACU07w3AAAZuUlEQVR4Ae3df4yU930n8Pcslu1I1PU17okdO1cLrTD+g8rNcvRyti6247K5NG5S5HOl5hA2uZ7du6RJEGYPTFy1Nv4RUJy0cWVkeQ9ErqqriHNrR8niZuVIbntBS886rBZWCGHVsNEFRQ5BloPCzGn2B+yzZMLyaP.
我不确定如何处理这些链接或如何下载图像。任何帮助都将不胜感激。
发布于 2020-12-10 21:56:17
您需要首先使用node.js Buffer解码来自base64的url。
// the content type image/png has to be removed first
const data = 'iVBORw0KGgoAAAANSUhEUgAAAw8AAADGCAYAAACU07w3AAAZuUlEQVR4Ae3df4yU930n8Pcslu1I1PU17okdO1cLrTD+g8rNcvRyti6247K5NG5S5HOl5hA2uZ7du6RJEGYPTFy1Nv4RUJy0cWVkeQ9ErqqriHNrR8niZuVIbntBS886rBZWCGHVsNEFRQ5BloPCzGn2B+yzZMLyaP';
const buffer = new Buffer(data);
const base64data = buff.toString('base64');
// after this you will get the url string and continue to fetch the image发布于 2020-12-10 21:57:20
这些是base64编码的图像(主要用于图标和小图像)。
你可以忽略它。
if(url.startsWith('data:')){
//base 64 image
} else{
// an image url
}如果你真的想搞乱base64,我可以给你一个变通办法。
import { parseDataURI } from 'dauria';
import mimeTypes from 'mime-types';
const fileContent = parseDataURI(file);
// you probably need an extension for that image.
let ext = mimeTypes.extension(fileContent.MIME) || 'bin';
fs.writeFile("a random file"+"."+ext, fileContent.buffer, function (err) {
console.log(err); // writes out file without error, but it's not a valid image
});https://stackoverflow.com/questions/65235347
复制相似问题