nodejs 爬图片

const path = require(‘path‘);
const request = require(‘request‘);
const cheerio = require(‘cheerio‘);
const fs =  require(‘fs‘);

let page = 0;
const total = 10; //总共几页
const waitTime = 30000; //间隔时间, 毫秒
const savePath = ‘./image‘;

begin();

function begin() {
    if (!fs.existsSync(savePath)) {
        fs.mkdirSync(savePath);
    }

    const url = ‘http://www.图片网址.com‘;
    getImageByUrl(url);
}

//爬取指定页面的图片
function getImageByUrl (_url) {
    page++;
    console.log(`开始第${page}页`);
    request(_url, function(err, resp, body) {
        if (err) {
            console.log(err);
        } else {
            const $ = cheerio.load(body);
            $(‘.commentlist > li .text img‘).each(function() {
                const src = $(this).attr(‘src‘);
                const baseName = path.win32.basename(src);
                request(`http:${src}`).pipe(fs.createWriteStream(`${savePath}/${baseName}`));
            });
            const prevUrl = $(‘.previous-comment-page‘).attr(‘href‘);
            if (page < total && prevUrl.length > 0) {
                setTimeout(() => {
                    getImageByUrl(`http:${prevUrl}`);
                }, waitTime);
            }
        }
    });
}

 

nodejs 爬图片

上一篇:apache配置文件说明


下一篇:新技术新框架不断涌现,目前学习web前端开发都要掌握什么?