Created
August 13, 2024 00:11
-
-
Save cccaaannn/05ab87bc04c98b31e689bb801f76af69 to your computer and use it in GitHub Desktop.
Download images with node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
const fs = require('fs'); | |
const imageUrls = [ | |
] | |
const padFileName = (index) => { | |
let str = index.toString(); | |
while (str.length < 3) { | |
str = '0' + str; | |
} | |
return str; | |
} | |
(() => { | |
let index = 1; | |
for (const url of imageUrls) { | |
const fileName = `img/${padFileName(index)}.png`; | |
const file = fs.createWriteStream(fileName); | |
https.get(url, response => { | |
response.pipe(file); | |
file.on('finish', () => { | |
file.close(); | |
console.log(`Downloaded: ${fileName}`); | |
}); | |
}).on('error', err => { | |
fs.unlink(fileName); | |
console.error(`Error: ${fileName} - ${err.message}`); | |
}); | |
index++; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment