如何用Node.js高效爬取并下载超过一万张图片?
- 内容介绍
- 文章标签
- 相关推荐
本文共计600个文字,预计阅读时间需要3分钟。
本篇文章与大家分享一个node实战案例,看看作者是如何用nodejs爬取了成千上万张小哥哥小姐姐的壁纸,希望对大家有所帮助!哈哈,大家好,我是小马,为什么要点这么多图片呢?前几天使用u
本篇文章给大家分享一个node实战,看看作者是如何用 nodejs 爬了一万多张小姐姐壁纸的,希望对大家有所帮助!
哈喽,大家好,我是小马,为什么要下载这么多图片呢? 前几天使用 uni-app + uniCloud 免费部署了一个壁纸小程序,那么接下来就需要一些资源,给小程序填充内容。
爬取图片首先初始化项目,并且安装 axios 和 cheerio
npm init -y && npm i axios cheerio
axios 用于爬取网页内容,cheerio 是服务端的 jquery api, 我们用它来获取 dom 中的图片地址;
const axios = require('axios') const cheerio = require('cheerio') function getImageUrl(target_url, containerEelment) { let result_list = [] const res = await axios.get(target_url) const html = res.data const $ = cheerio.load(html) const result_list = [] $(containerEelment).each((element) => { result_list.push($(element).find('img').attr('src')) }) return result_list }
这样就可以获取到页面中的图片 url 了。接下来需要根据 url 下载图片。
方式一:使用内置模块 ‘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}/美女`
await download(item.wp, filePath, {
filename: item.id + '.jpeg',
headers,
}).then(() => {
console.log(`Download ${item.id} Completed`)
return
})
}
}
load() 上面代码中先要设置 直接 、 微信小程序搜索 “西瓜图库” 体验。 p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c5301b8b97094e92bfae240d7eb1ec5e~tplv-k3u1fbpfcp-zoom-1.awebp? 更多node相关知识,请访问:nodejs 教程!User-Agent 并且设置 3s 延迟, 这样可以防止服务端阻止爬虫,直接返回 403。node index.js 就会自动下载图片了。
本文共计600个文字,预计阅读时间需要3分钟。
本篇文章与大家分享一个node实战案例,看看作者是如何用nodejs爬取了成千上万张小哥哥小姐姐的壁纸,希望对大家有所帮助!哈哈,大家好,我是小马,为什么要点这么多图片呢?前几天使用u
本篇文章给大家分享一个node实战,看看作者是如何用 nodejs 爬了一万多张小姐姐壁纸的,希望对大家有所帮助!
哈喽,大家好,我是小马,为什么要下载这么多图片呢? 前几天使用 uni-app + uniCloud 免费部署了一个壁纸小程序,那么接下来就需要一些资源,给小程序填充内容。
爬取图片首先初始化项目,并且安装 axios 和 cheerio
npm init -y && npm i axios cheerio
axios 用于爬取网页内容,cheerio 是服务端的 jquery api, 我们用它来获取 dom 中的图片地址;
const axios = require('axios') const cheerio = require('cheerio') function getImageUrl(target_url, containerEelment) { let result_list = [] const res = await axios.get(target_url) const html = res.data const $ = cheerio.load(html) const result_list = [] $(containerEelment).each((element) => { result_list.push($(element).find('img').attr('src')) }) return result_list }
这样就可以获取到页面中的图片 url 了。接下来需要根据 url 下载图片。
方式一:使用内置模块 ‘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}/美女`
await download(item.wp, filePath, {
filename: item.id + '.jpeg',
headers,
}).then(() => {
console.log(`Download ${item.id} Completed`)
return
})
}
}
load() 上面代码中先要设置 直接 、 微信小程序搜索 “西瓜图库” 体验。 p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c5301b8b97094e92bfae240d7eb1ec5e~tplv-k3u1fbpfcp-zoom-1.awebp? 更多node相关知识,请访问:nodejs 教程!User-Agent 并且设置 3s 延迟, 这样可以防止服务端阻止爬虫,直接返回 403。node index.js 就会自动下载图片了。

