概述
const download = require('download')
const axios = require('axios')
let headers = {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
}
function sleep(time) {
return new Promise((reslove) => setTimeout(reslove, time))
}
async function load(skip = 0) {
const data = await axios
.get(
'http://service.picasso.adesk.com/v1/vertical/category/4e4d610cdf714d2966000000/vertical',
{
headers,
params: {
limit: 30, // 每页固定返回30条
skip: skip,
first: 0,
order: 'hot',
},
}
)
.then((res) => {
return res.data.res.vertical
})
.catch((err) => {
console.log(err)
})
await downloadFile(data)
await sleep(3000)
if (skip < 1000) {
load(skip + 30)
} else {
console.log('下载完成')
}
}
async function downloadFile(data) {
for (let index = 0; index < data.length; index++) {
const item = data[index]
// Path at which image will get downloaded
const filePath = `${__dirname}/img`
await download(item.wp, filePath, {
filename: item.id + '.jpeg',
headers,
}).then(() => {
console.log(`Download ${item.id} Completed`)
return
})
}
}
load()
视频
let mapUrl = 'https://vd2.bdstatic.com/mda-kimq0720banx2sm7/v1-cae/sc/mda-kimq0720banx2sm7.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1676945800-0-0-819ef8908b65d75bac11d490ead528a2&bcevod_channel=searchbox_feed&pd=1&cd=0&pt=3&logid=2800780637&vid=9375399638589320789&abtest=107354_1&klogid=2800780637'
const download = require('download')
const filePath = `${__dirname}/video`
let headers = {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
}
download(mapUrl, filePath, {
filename: 'video' + '.mp4',
headers,
}).then(() => {
console.log(`Download Completed`)
return
})
最后
以上就是快乐马里奥为你收集整理的node下载图片、视频的全部内容,希望文章能够帮你解决node下载图片、视频所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复