-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.js
More file actions
36 lines (28 loc) · 709 Bytes
/
crawler.js
File metadata and controls
36 lines (28 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//爬虫
var http =require('http');
var url ='http://www.imooc.com/learn/348';
var cheerio =require('cheerio');
function filterChapters(html){
var $ = cheerio.load(html)
var chapters = $('.chapter')
// console.log(chapters);
var courseData =[];
chapters.each(function(item){
var chapter = $(this);
var chapterTitle = chapter.find('strong').text();
courseData.push(chapterTitle)
})
return courseData
}
http.get(url,function(res){
var html =''
res.on('data',function(data){
html += data
});
res.on('end',function(){
var courseData = filterChapters(html);
console.log(courseData)
}).on('error',function(){
console.log('获取出错')
});
})