node.js 爬取图片

/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 *
 * Created by Ning on 2018/8/16.
 */
var http = require("http");
var fs = require("fs");
var path = require("path");

var url = "http://www.xxxxxxxx.cn/";
http.get(url,function (res) {

    var content = '';
    res.on('data',function (data) {
        content += data;
    });
    res.on('end',function () {
        //正则匹配jpg png图片
        var reg = /src="(.*?\.jpg|.*?\.png)"/img;
        var filename = "";
        //循环匹配
        while (filename = reg.exec(content)){
            getImage(url,filename[1]);
           // url = "http://hospital.dev.ftbj.net" + filename[1];
        }
    });
});

//爬取图片
function getImage(url,filename) {
    //获取图片的文件名
    var obj = path.parse(filename);
    var fn = obj.base;
    var streaml = fs.createWriteStream('./image/'+fn);
    var image = url + filename;
    http.get(image,function (res) {
        //管道的方式流入文件中
        res.pipe(streaml);
    });
}

 

posted @ 2018-08-17 11:07  宁佳兵  阅读(422)  评论(0编辑  收藏  举报