Skip to content

Instantly share code, notes, and snippets.

@kingwrcy
Created April 10, 2015 05:33
Show Gist options
  • Save kingwrcy/c0c2f07ec70d284af121 to your computer and use it in GitHub Desktop.
Save kingwrcy/c0c2f07ec70d284af121 to your computer and use it in GitHub Desktop.
my wife need some imgs on http://www.tjftbc.com , so i have made this script,backup here.
var cheerio = require('cheerio');
var request = require('request');
var fs = require('fs');
var baseurl = 'http://www.tjftbc.com/product/';
var $,category = [];
request(baseurl,function(error,response,body){
if (!error && response.statusCode == 200) {
$ = cheerio.load(body);
var cates = $('.productclass_dolphin>a');
for (var i = 1; i < cates.length; i++) {
// if(i>0)break;
create_category({
name:cates.eq(i).text(),
url:baseurl+cates.eq(i).attr('href').substring(11)
});
};
}
});
function start(cate){
request(cate.url,function(error,response,body){
var p = cheerio.load(body);
var pagenow = p('li.pagesnow');
save_img(p('#Gallery img'),cate.name);
while(!pagenow.next().is('.pbutton')){
start({
name:cate.name,
url:baseurl + pagenow.next().find('a').attr('href').substring(9)
});
pagenow = pagenow.next();
}
})
}
function save_img(imgs,name){
for (var i = 0; i < imgs.length; i++) {
var imgurl = baseurl + imgs.eq(i).attr('src').substring(14);
var imgname = imgs.eq(i).attr('src').split("/")[5];
request(imgurl).pipe(fs.createWriteStream('/tmp/xia/'+name+'/'+imgname))
}
}
function create_category(cate){
fs.mkdir('/tmp/xia/'+cate.name,function(){
start(cate);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment