node小爬虫

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node小爬虫相关的知识,希望对你有一定的参考价值。

本身就想玩一下爬虫这玩意,看到http://www.imooc.com/video/7965,诺,关于node的爬虫视频,就省的自己研究了,当然,视频中爬的html和现在的有不同,并不是很影响。

为了复习一下fs模块,就爬了之后写成自己想要的形式。

将文件爬出来,并把需要的部分存成自己想要的格式,然后写入一个txt文件,当然,后期通过这也可以写入数据库,想怎么玩就怎么玩了。以下是爬出来的文件。

技术分享

小伙伴肯定迫不及待看源代码了吧。

直接贴源代码了:

--------------------------------------------------------------------我是分割线

var http = require(‘http‘);
var fs = require(‘fs‘);
var util = require(‘util‘);
var cheerio = require(‘cheerio‘);

var imooc_url = ‘http://www.imooc.com/learn/348‘;


function filterChapters(html) {
fs.stat(‘crawler.html‘, function(err, stats) {
if (err) {
fs.writeFile(‘crawler.html‘, html, function(err) {
if (err) return console.log(err);
console.log(‘写入成功!‘);
});
} else {
console.log(‘文件已存在‘);
}
});
var $ = cheerio.load(html);
var chapters = $(‘.chapter‘);
// [{
// chapterTitle:‘‘,
// videos:[{
// title:‘‘,
// id:‘‘
// },{
// title:‘‘,
// id:‘‘
// }]
// }]
var courseData = [];
chapters.each(function(item) {
var chapter = $(this);
var chapterTitle = chapter.find(‘strong‘).text().replace(/\\s+/g,‘ ‘);
var videos = chapter.find(‘.video‘).children(‘li‘);
var chapterData = {
chapterTitle: chapterTitle,
videos: []
}
videos.each(function(item) {
var video = $(this).find(‘.J-media-item‘);
var videoTitle = video.text().replace(/\\s+/g,‘ ‘).replace(‘开始学习‘,‘‘);
console.log(videoTitle);
var id = video.attr(‘href‘).split(‘video/‘)[1].replace(/\\s+/g,‘ ‘);
chapterData.videos.push({
title: videoTitle,
id: id
});
});
courseData.push(chapterData);
});
return courseData;
}


function printCourseInfo(courseData) {
//console.log(util.inspect(courseData));
courseData.forEach(function(item) {
var chapterTitle = item.chapterTitle;
fs.appendFileSync(‘crawler.txt‘, chapterTitle);
fs.appendFileSync(‘crawler.txt‘, ‘\\n‘);
item.videos.forEach(function(video) {
var v = ‘ \\t 【 ‘ + video.id + ‘ 】 ‘ + video.title;
fs.appendFileSync(‘crawler.txt‘, v);
fs.appendFileSync(‘crawler.txt‘, ‘\\n‘);
})

});


}

http.get(imooc_url, function(res) {
var html = ‘‘;
res.on(‘data‘, function(data) {
html += data;
});
res.on(‘end‘, function() {
var courseData = filterChapters(html);
printCourseInfo(courseData);
});

}).on(‘error‘, function() {
console.log(‘获取出错‘);
})

--------------------------------------------------------------------------我是分割线

http.get(imooc_url, function(res) {
var html = ‘‘;
res.on(‘data‘, function(data) {
html += data;
});
res.on(‘end‘, function() {
var courseData = filterChapters(html);
printCourseInfo(courseData);
});

}).on(‘error‘, function() {
console.log(‘获取出错‘);
})

显示请求了慕课的一个网址,然后把数据保存为变量,传给章节过滤函数,然后打印信息。

过滤函数中,为了方便我查看,先是把它存为html文件。

fs.stat(‘crawler.html‘, function(err, stats) {
if (err) {
fs.writeFile(‘crawler.html‘, html, function(err) {
if (err) return console.log(err);
console.log(‘写入成功!‘);
});
} else {
console.log(‘文件已存在‘);
}
});

加载cheerio模块,构建自己需要的数据,由于跟jquery操作差不多,就不需要多解释了。

打印的时候将其以同步追加的方式写入txt文件中,

courseData.forEach(function(item) {
var chapterTitle = item.chapterTitle;
fs.appendFileSync(‘crawler.txt‘, chapterTitle);
fs.appendFileSync(‘crawler.txt‘, ‘\\n‘);
item.videos.forEach(function(video) {
var v = ‘ \\t 【 ‘ + video.id + ‘ 】 ‘ + video.title;
fs.appendFileSync(‘crawler.txt‘, v);
fs.appendFileSync(‘crawler.txt‘, ‘\\n‘);
})

});

//如果慕课网这个页面又变了就得改改构造数据的那一块了。

现在的chapter结构是这样的

<div class="chapter ">
<!-- 章节标题 -->
<h3>

<span class="icon-drop_down js-close"></span>
<strong>
<i class="icon-chapter"></i>
第1章 前言
<div class="icon-info chapter-info">
<i class="icon-drop_up triangle">
<div class="chapter-introubox">
<div class="chapter-content">带你了解为什么要学习 Nodejs。</div>
</div>
</i>
</div>
</strong>

</h3>
<!-- 章节标题 end -->
<!-- 章节小节 -->
<ul class="video">
<li data-media-id="6687">
<a href=‘/video/6687‘ class="J-media-item">
<i class="icon-video type"></i>
1-1 Node.js基础-前言
(01:20)

<button class="r moco-btn moco-btn-red preview-btn">开始学习</button>

</a>
<!-- 未登录时 -->
<!-- <a target="_blank" href="/video/1430" class="J-media-item studyvideo">1-1 Java简介 (05:49)
<button class="r moco-btn moco-btn-blue preview-btn">预览</button>
</a> -->
</li>
<li data-media-id="6688">
<a href=‘/video/6688‘ class="J-media-item">
<i class="icon-video type"></i>
1-2 为什么学习Nodejs
(05:43)

<button class="r moco-btn moco-btn-red preview-btn">开始学习</button>

</a>
<!-- 未登录时 -->
<!-- <a target="_blank" href="/video/1430" class="J-media-item studyvideo">1-1 Java简介 (05:49)
<button class="r moco-btn moco-btn-blue preview-btn">预览</button>
</a> -->
</li>
</ul>
<!-- 章节小节 end -->
</div>

 
























































































































































以上是关于node小爬虫的主要内容,如果未能解决你的问题,请参考以下文章

node小爬虫

Node.js之网络小爬虫

node小爬虫

一个Node.js的小爬虫

Node.js umei图片批量下载Node.js爬虫1.00

node--http小爬虫&事件模块