Node 做个简单小爬虫
你也想做这个小demo?
总共有四步:
- 安装node
- 复制一下代码
- node运行下
- localhost:3001直接访问
很简单,想即可操作~
let http = require('http');
http.createServer(function(req, res) {
let client = http.request({
host: 'yuedu.163.com',
method: 'get',
port: '80'
}, function(db) {
let bookArr = [];
db.on('data', function(data) {
bookArr.push(data);
});
db.on('end', function() {
let result = Buffer.concat(bookArr).toString();
let matches = result.match(/<div class="category">([\s\S]*?)<\/div>/gm);
res.setHeader('Content-Type', 'text/html;charset=utf8');
res.end(matches.join(''));
})
});
client.end();
}).listen(3001);