Node.Js 简单的数据采集示例,使用 crawler
源码如下:
// 文件名:collect-crawler.js
/**
* Node.Js 简单的数据采集示例,使用 crawler
* 安装依赖:npm install crawler
* 注意事项:gzip页面处理 gzip:true
*/
const NmCrawler = require('crawler');
const NmFs = require('fs');
var c = new NmCrawler({
maxConnections: 10,
callback: function (error, res, done) {
if (error) {
console.log(error);
} else {
var $ = res.$;
console.log($('title').text());
NmFs.writeFile('./collect-crawler-163.html', res.body, () => {
console.log('write success');
})
}
done();
}
})
c.queue([{
uri: 'http://www.163.com',
// forceUTF8: true,
// incomingEncoding: 'GBK'
}])
c.queue([{
uri: 'https://pic2020.lianzhixiu.com/2016/1123/19/2.jpg',
filename: "./img/collect-crawler-2.png",
encoding: null,
jQuery: false,
callback: function (error, res, done) {
if (error) {
console.log(error);
} else {
NmFs.createWriteStream(res.options.filename).write(res.body);
console.log('===', res.body.length, 'bytes');
}
done()
}
}])
运行:
node collect-crawler.js