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);

该博客介绍了如何使用Node.js编写一个简单的网络爬虫,通过GET请求抓取yuedu.163.com的数据,并利用正则表达式匹配特定内容。步骤包括安装Node.js、编写代码、运行程序及访问localhost:3001查看结果。这是一个适合初学者的Node.js实战教程。
1万+

被折叠的 条评论
为什么被折叠?



