Node.js 爬蟲初探


前言

      在學習慕課網視頻和Cnode新手入門接觸到爬蟲,說是爬蟲初探,其實並沒有用到爬蟲相關第三方類庫,主要用了node.js基礎模塊http、網頁分析工具cherrio。 使用http直接獲取url路徑對應網頁資源,然后使用cherrio分析。 這里我主要是把慕課網教學視頻提供的案例自己敲了一邊,加深理解。在coding的過程中,我第一次把jq獲取后的對象直接用forEach遍歷,直接報錯,是因為jq沒有對應的這個方法,只有js數組可以調用。

 

知識點

    ①:superagent抓去網頁工具。我暫時未用到。

    ②:cherrio 網頁分析工具,你可以理解其為服務端的jQuery,因為語法都一樣。

 

效果圖

1、抓取整個網頁

 

2、分析后的數據, 我這里是以慕課網提供的示例為案例實現的例子。

 

爬蟲初探源碼分析

var http=require('http');
var cheerio=require('cheerio');

var url='http://www.imooc.com/learn/348';

/****************************
打印得到的數據結構
[{
	chapterTitle:'',
	videos:[{
		title:'',
		id:''
	}]
}]
********************************/
function printCourseInfo(courseData){
	courseData.forEach(function(item){
		var chapterTitle=item.chapterTitle;
		console.log(chapterTitle+'\n');
		item.videos.forEach(function(video){
			console.log(' 【'+video.id+'】'+video.title+'\n');
		})
	});
}


/*************
分析從網頁里抓取到的數據
**************/
function filterChapter(html){
	var courseData=[];

	var $=cheerio.load(html);
	var chapters=$('.chapter');
	chapters.each(function(item){
		var chapter=$(this);
		var chapterTitle=chapter.find('strong').text(); //找到章節標題
		var videos=chapter.find('.video').children('li');

		var chapterData={
			chapterTitle:chapterTitle,
			videos:[]
		};

		videos.each(function(item){
			var video=$(this).find('.studyvideo'); 
			var title=video.text();
			var id=video.attr('href').split('/video')[1];

			chapterData.videos.push({
				title:title,
				id:id
			})
		})

		courseData.push(chapterData);
	});

    return courseData;
}

http.get(url,function(res){
	var html='';

	res.on('data',function(data){
		html+=data;
	})

	res.on('end',function(){
		var courseData=filterChapter(html);
		printCourseInfo(courseData);
	})
}).on('error',function(){
	console.log('獲取課程數據出錯');
})

 

參考資料

https://github.com/alsotang/node-lessons/tree/master/lesson3

http://www.imooc.com/video/7965


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM