類比的是 http://www.oschina.net/code/snippet_226106_23414 這個python程式
只是用node.js翻寫一遍,練習練習
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 = thiswhile (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);});});
1) 用到了node-jquery需要VC++2010的支援。所以通過NPM下包的時候先確認有VC++2010(用到VCbuild.bat,別拿netframework4.0裡的,沒用)
2)修改儲存目錄