Created
December 30, 2022 03:32
-
-
Save weisiwu/21ee11f86538a4ab9d9fca3530bbabc5 to your computer and use it in GitHub Desktop.
node 下载图片 -- 1
This file contains hidden or 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
// 1、内置https+fs下载 | |
const fs = require('fs') | |
const https = require('https') | |
const url = 'GFG.jpeg' | |
https.get(url, (res) => { | |
const path = `${__dirname}/files/img.jpeg` | |
const filePath = fs.createWriteStream(path) | |
res.pipe(filePath) | |
filePath.on('finish', () => { | |
filePath.close() | |
console.log('Download Completed') | |
}) | |
}) | |
// 2、download下载图片 | |
const download = require('download') | |
const file = 'GFG.jpeg' | |
const filePath = `${__dirname}/files` | |
download(file, filePath).then(() => { | |
console.log('Download Completed') | |
}) | |
// 3、node-downloader-helper下载 | |
const { DownloaderHelper } = require('node-downloader-helper') | |
const file = 'GFG.jpeg' | |
const filePath = `${__dirname}/files` | |
const dl = new DownloaderHelper(file, filePath) | |
dl.on('end', () => console.log('Download Completed')) | |
dl.start() |
This file contains hidden or 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
{ | |
"devDependencies": { | |
// 从网页获取图片地址列表的包 | |
"axios": "4.2", | |
"cheerio": "^11.0.0", | |
// 下载图片的包 | |
"node-downloader-helper": "10.2", // 监听事件得知下载结束 | |
"download": "^8.3.17" // Promise形式,结束后then中接受到值 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment