const axios = require('axios')
const cheerio = require('cheerio')
const download = require('download')
function sleep (time) {
return new Promise((reslove) => setTimeout(reslove, time))
}
async function load (skip = 0, number = 2) {
let baseUrl = `https://www.xxxxx/update_${number}.html`
const imgUrlList = await axios.get(baseUrl).then(res => {
let imgList = []
let $ = cheerio.load(res.data)
$('.wrapper .w790 .touxiangbox ul li a img').each((i, ele) => {
let imgInfo = {
title: $(ele).attr('alt'),
url: $(ele).attr('src')
}
imgList.push(imgInfo)
})
return imgList
}).catch((err) => {
console.log(err)
})
await downloadFile(imgUrlList)
await sleep(3000)
if (skip < 2) {
load(skip + 1, number + 1)
} else {
console.log('下载完成')
}
}
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',
}
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.url, filePath, {
filename: item.title + '.gif',
headers,
}).then(() => {
console.log(`Download ${item.title} Completed`)
return
})
}
}
load()
js 爬虫初战
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 1. 破解返回结果 1.1. 分析 response 打开网站,在输入框输入查询关键字【吴*凡】,分析 respo...
- 前言 前面一章讲的是模拟登录,留了一个模拟密码加密还没讲。 因为这一过程的调试探索还是蛮多内容的,我更倾向于记录自...
- 原文链接:http://www.php.cn/div-tutorial-360016.html 从左到右 / 渲染...
- 首先说一下什么是加密,所谓js加密大多出现在表单提交过程中,下面我将以中国电信为例,详细讲解如何利用pyv8来加密...