Skip to content

Instantly share code, notes, and snippets.

@weisiwu
Created December 30, 2022 03:32
Show Gist options
  • Save weisiwu/21ee11f86538a4ab9d9fca3530bbabc5 to your computer and use it in GitHub Desktop.
Save weisiwu/21ee11f86538a4ab9d9fca3530bbabc5 to your computer and use it in GitHub Desktop.
node 下载图片 -- 1
// 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()
{
"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