引用(爬虫)不给稿费的,切你jjhtml
在上一篇post(http://www.cnblogs.com/zeusro/p/4185196.html)里面,我留下了3个坑没有填平。node
2模块化jquery
加载到nodejs里面,用于批量采集。git
方法:把变更的参数作成github
3淘宝的反采集sql
4数据的持久化dom
其实呢,还有一个,就是中文编码的问题,nosql
那么今天把其中2个解决了。持久化给个思路,反采集就算了,哥已经宣布弃坑了。async
var system = require('system'); var url = system.args[1]; var filename = system.args[2];
第一个参数是咱们运行的js,第二个参数开始才是咱们真正用获得的。模块化
phantomjs phantomcapture.js http://shop115235781.m.taobao.com/#list av
这样就简单完成了模块化。
那么在node那边,咱们要作到的就是,要引用并运行这个模块。这里我用了node的child_process,每个url开启一个子进程。
这个才是我宣布弃坑的缘由。我在每次调用waitfor超时以后都有去截屏处理的。而我开那么多个子进程捕获到的结果就是,要登陆淘宝,还得输入验证码。。。要输入验证码,呵呵....
楼主用SqlServer惯的。
node.js确实有连SqlServer的驱动(https://github.com/Azure/node-sqlserver),问题是那玩意我链接失败了。那个项目2年前最后一次提交代码。做者本身也说了,说是去放假了解决不了一些issue,其实看起来更像是弃坑的节奏。
那么我只能建议连芒果DB或者nosql了。
这个有人在私信里面问我。这个我其实一早就预料到了,由于以前在找资料的时候就有提到js的编码问题,这个是固有缺陷。解决这个问题,引入iconv-lite就好了。
这里,我使用了这玩意扩展了node的编码
// After this call all Node basic primitives will understand iconv-lite encodings. iconv.extendNodeEncodings();
而后在进程通讯那里
var buf = new Buffer(data, 'win1251'); buf.write(data, 'gbk'); console.log(buf.toString('gbk'));
这样就好了
主进程node的代码以下
var colors = require('colors') , jsdom = require('jsdom').jsdom , async = require('async') , http = require('http') , fs = require('fs') , jquery = fs.readFileSync("jquery-1.10.2.min.js", "utf-8") , iconv = require('../node_modules/iconv-lite') , phantom = require('phantomjs'); // After this call all Node basic primitives will understand iconv-lite encodings. iconv.extendNodeEncodings(); var count = 0; console.log('主进程开启'); var startTime = new Date().getTime(); var urls = new Array( "http://shop100338207.m.taobao.com/#list", "http://shop68291879.m.taobao.com/#list", "http://shop115235781.m.taobao.com/#list", "http://shop10199638.m.taobao.com/#list", "http://shop67272667.m.taobao.com/#list", "http://shop109683760.m.taobao.com/#list", "http://shop33495993.m.taobao.com/#list", "http://shop58501945.m.taobao.com/#list", "http://shop62907168.m.taobao.com/#list", "http://shop59495864.m.taobao.com/#list", "http://shop60374631.m.taobao.com/#list" ); for (var i = 0; i < urls.length; i++) { console.log(("采集地址:" + urls[i]).red); capture(urls[i]); } function capture(url) { count++; var spawn = require('child_process').spawn, ls = spawn('phantomjs', ['phantomcapture.js', url, count]); ls.stdout.on('data', function (data) { var buf = new Buffer(data, 'win1251'); buf.write(data, 'gbk'); console.log(buf.toString('gbk')); }); ls.stderr.on('data', function (data) { //console.log('stderr: ' + data); }); ls.on('close', function (code) { if (code == 1) { console.log('child process异常结束。目标:' + url); } }); }
1在window平台上编译一些模块的时候要用到VS2010的MSbuild,我原本装2013的,可是不行。这货就是这么贱,要10版本的MSbuild,因此我只能老老实实去装个10版本了
最后啰嗦一下,若是出现啥不是内部或外部命令之类的,那确定是你打开方式不对,没有弄系统环境变量。
引用(爬虫)不给稿费的,切你jj
Node.js中的child_process及進程通讯
iconv-lite