基于node.js的网页抓捕
2016-10-13 15:27
288 查看
从无到有耗时2天,蓝馊!香菇!
var tmp = ''; var count = 0; var url = 'http://data.eastmoney.com/zjlx/600050.html'; var http = require('http'); var iconv = require('iconv-lite'); var req = http.request(url, function(res){ //从网页上取数据 res.on('data',function(data){ count++; data = iconv.decode(data, 'GBK'); tmp += data; console.log('Count\t'+count); }); res.on('end', function() { if(tmp){ //writeFile('a.txt',tmp); tmp = replace(tmp); //writeFile('b.txt',tmp); var cheerio = require('cheerio'), $ = cheerio.load(tmp); tmp = $('#content_zjlxtable').html(); writeFile('c.html',tmp); //tmp = TableToCsv(tmp); //writeFile('d.csv',tmp); }else{ console.log('Empty'); } }); }); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); req.end(); //写文件 function writeFile(file,str){ var fs = require('fs'); fs.writeFile(file, str, function(err){ if(err)console.log("fail " + err); }); } function replace(source){ var rep0 = /'/g; var rep1 = /\r\n/g;//MLGB原来少个/r,win是/r/n,linux是/r var rep2 = /<!--.*?-->/ig; var rep3 = /\/\*.*?\*\//ig; var rep4 = /[ ]+</ig; var source0 = source.replace(rep0,'\"'); var source1 = source0.replace(rep1,''); var source2 = source1.replace(rep2,''); var source3 = source2.replace(rep3,''); var source4 = source3.replace(rep4,'<'); return source4; }///<[^>]+>/g; function TableToCsv(source){ var rep0 = /\s/g; var rep1 = /<\/th>/g; var rep2 = /<\/td>/g; var rep3 = /<\/tr>/g; var rep4 = /<[^>]+>/g; var rep5 = / /g; var source0 = source.replace(rep0,''); var source1 = source0.replace(rep1,','); var source2 = source1.replace(rep2,','); var source3 = source2.replace(rep3,'\r\n'); var source4 = source3.replace(rep4,''); var source5 = source4.replace(rep5,''); return source5; }
相关文章推荐
- 基于node.js的快速开发透明代理
- 基于Node.js的Web Socket
- javascript,第一个基于node.js的Http服务
- 使用Node.js + Mongodb构建基于Cloud Foundry的项目
- node.js jsdom gb系列网页中文乱码问题解决方案
- 使用js实现基于可视布局信息的网页噪音去除的测试方法
- 基于JavaScript(Node.js)的HashSet实现
- node.js基于工厂方法的mysql模块封装
- 基于jQuery和flash使用js接口的网页mp3音乐播放器 -- JPlayer 用法详解
- 基于Node.js的自动化构建工具Grunt.js
- 基于WebSocet简单聊天室(NodeJS + node-websocket-server)
- Node.js基于Google V8提供了基于事件的I/O处理
- 完全傻瓜式搭建基于nodejs+express+vCloudFoundry的网站
- Pomelo:网易开源基于 Node.js 的游戏服务端框架
- 基于Node.js服务器的WebSocket Video
- 基于node.js的快速开发透明代理
- Dnode:基于Node.js给浏览器提供异步远程方法调用
- Node.js爬虫--网页请求模块
- 基于node.js的构建工具grunt.js
- 基于Node.js的Web框架集合