- //
- // By hmmabc@OSC
- //
- var fs = require('fs');
- var mkdirp = require('mkdirp');
- var request = require('request');
- var $ = require('jquery').create();
- //萌妹吧
- //var url = "http://tieba.baidu.com/f?ie=utf-8&kw=%E8%90%8C%E5%A6%B9%E5%AD%90";
- //美女吧
- //var url = "http://tieba.baidu.com/f?ie=utf-8&kw=%E7%BE%8E%E5%A5%B3";
- //黑丝吧
- var url = "http://tieba.baidu.com/f?ie=utf-8&kw=%E9%BB%91%E4%B8%9D";
- var baseUrl = "http://tieba.baidu.com";
- var posts = [];
- var count = 0;
- $.get(url,function(data){
- $(data).find("a.j_th_tit").each(function(){
- posts.push({title: this.title,url: baseUrl + $(this).attr("href")});
- });
- console.log("共有帖子"+posts.length);
- $.each(posts,function(index,post){
- downpost(post.url);
- });
- });
- var downpost = function(url){
- $.get(url,function(data){
- var imgs = [];
- var dir = "";
- $(data).find(".BDE_Image").each(function(){
- imgs.push(this.src);
- });
- if(imgs.length == 0) return;
- dir = url.match(/\\d+$/)[0];
- mkdirp(dir,function(err){
- if(err) console.log(err);
- });
- $.each(imgs,function(index,value){
- download(value,dir,Math.floor(Math.random()*100000)+ value.substr(-4,4));
- });
- console.log("已完成" + ++count);
- });
- }
- var download = function(uri, dir,filename){
- request.head(uri, function(err, res, body){
- request(uri).pipe(fs.createWriteStream(dir + "/" + filename));
- });
- };
- //该片段来自于http://www.codesnippet.cn/detail/191220138142.html
来源: http://www.codesnippet.cn/detail/191220138142.html