抓取页面图片并批量下载

481 阅读1分钟

抓取页面图片并批量下载

有时候喜欢访问的页面图片比较多。一个个下载比较麻烦。那就写个脚本把。

const axios = require('axios');
const fs = require('fs');
const path = require('path');
// 请求的页面地址
const httpUrl = '';
// 存放图片的地址
let dirName = path.join(__dirname, 'images');

if (!fs.existsSync(dirName)) {
  console.log('文件夹已存在');
} else {
  fs.mkdir(dirName, () => {
    console.log('文件夹创建成功');
  });
}

async function getImages(url) {
  let htmlData = await axios.get(url);
  const reg = /http\b.*?(?:0\\)/g
  const data = htmlData.data.match(reg)
  downLoadImg(data, 1)
}

function downLoadImg (data, index) {
  const item = data[index]
  if (!item) return
  const str = path.parse(item).dir
  axios.get(str + '/0', { responseType: 'stream' }).then((res) => {
    const fileType = res.data.headers['content-type'].split('/')[1]
    const srcFile = path.join(dirName, `${Date.now()}-${index}` + '.' + fileType)
    let stream = fs.createWriteStream(srcFile)
    res.data.pipe(stream)
    res.data.on('close', () => {
      stream.close()
      console.log('图片' + srcFile + '已经下载完成');
      if (index > data.length) return
      downLoadImg(data, ++index)
    })
  }).catch((r) => {
    console.log('失败:', index)
    if (index > data.length) return
    downLoadImg(data, ++index)
  })
}

getImages(url)