返回顶部

收藏

抓妹子图

更多

模拟Pyhton的代码,使用node.js从百度姐脱吧抓妹子图。

使用了node-jquery, NPM添加的时候需要VC++2010的支持。

var baidu_base_url = 'http://tieba.baidu.com';

var http = require('http');
var url = require('url').parse(baidu_base_url + '/f?ie=utf-8&kw=%E5%A7%90%E8%84%B1');
var iconv = require('iconv-lite');
var BufferHelper = require('bufferhelper');
var $ = require('jquery');
var _ = require('underscore');
var saveRoot = "O:\\baidu\\"; //
var fs = require('fs');

String.prototype.replaceAll = function(s1, s2) {
    var demo = this
    while (demo.indexOf(s1) != - 1)
    demo = demo.replace(s1, s2);
    return demo;
}

function htmlBufferget(Url, callback) {
    http.get(Url, function(res) {
        var bufferHelper = new BufferHelper();
        res.on('data', function(chunk) {
            bufferHelper.concat(chunk);
        });
        res.on('end', function() {
            callback(bufferHelper.toBuffer());
        });
    });
}

//
function htmlget(Url, callback) {
    http.get(Url, function(res) {
        var bufferHelper = new BufferHelper();
        res.on('data', function(chunk) {
            bufferHelper.concat(chunk);
        });
        res.on('end', function() {
            var html = iconv.decode(bufferHelper.toBuffer(), 'GBK');
            callback($(html));
        });
    });
}

// 
function getTieZiImg(url, fold) {
    htmlget(url, function(html) {
        html.find('img.BDE_Image').each(function(i, el) {
            var $img = $(el);
            var imgUrl = $img.attr('src');
            htmlBufferget(imgUrl, function(buffer) {
                var fileName = fold + "/" + i + ".png";
                fs.writeFile(fileName, buffer, 'binary', function(err) {
                    if (err) throw err ;
                    console.log(fileName + '  File saved.');
                });
            });
        });
    });
}

//
htmlget(url, function(html) {
    html.find('a.j_th_tit').each(function(i, el) {
        var $a = $(el);
        var tieziUrl = baidu_base_url + $a.attr("href");
        var foldID = $a.attr("href").toString().replaceAll("/p/", "");
        var title = $a.html();
        //
        title = title.replaceAll("/", "");
        title = title.replaceAll("?", "");
        title = title.replaceAll(",", "");
        title = title.replaceAll(":", "");

        //for
        (function(tieziUrl, title, foldID) {
            _.delay(function() {
                var fold = saveRoot + title;
                var exists = fs.existsSync(fold);
                console.log(fold);
                if (!exists) {
                    console.log("" + fold);
                    fs.mkdirSync(fold);
                }
                getTieZiImg(tieziUrl, fold);
            },
            10);
        })(tieziUrl, title, foldID);
    });
});
//该片段来自于http://outofmemory.cn

标签:javascript,网络

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2014-07-02 13:51:05网页开发,HTML下载按钮 by arden
  2. 2015-02-03 09:42:10javascript 获取URL变量 by arden
  3. 2014-07-19 11:24:16Cookies 操作类 by 龙堂修罗
  4. 2014-08-08 21:32:11收藏到花瓣的代码 by starstroll
  5. 2014-08-27 13:42:29JS 3D相册代码分享 by 龙堂修罗
  6. 2014-11-23 11:59:22jQuery 获取URL请求参数 by starstroll
  7. 2014-06-11 16:37:52用iframe实现异步上传 by 林峰
  8. 2014-06-13 14:39:44使用JS进行目录上传(相当于批量上传) by 林峰
  9. 2014-06-20 14:22:18使用jQuery调用Asp.Net WebService by GauSir
  10. 2014-08-13 13:17:58JavaScript 直接对 Cookie 进行读写 by shenspring
  11. 2014-08-17 14:47:02javascript mvc by 林峰

发表评论