-
Notifications
You must be signed in to change notification settings - Fork 0
/
reptile.js
58 lines (55 loc) · 1.2 KB
/
reptile.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* @Author: black
* @Date: 2017-03-30 15:38:12
* @Last Modified by: black
* @Last Modified time: 2017-03-30 19:11:42
*/
var http = require('http')
var cheerio = require('cheerio')
var url = 'http://www.imooc.com/learn/348'
var courseData = []
//设计数据结构如下:
// [{
// articleTitle:'',
// video:[
// {
// vTitle:'',
// vid: ''
// }
// ]
// }]
function handlData(html) {
var $ = cheerio.load(html);
var chapters = $('.mod-chapters').children();
chapters.each(function(el) {
var self = $(this)
var articleTitle = self.find('strong').text().replace(/\s/g,'');
var vs = self.find('.video').children();
var article = {
articleTitle: articleTitle,
videos: []
}
vs.each(function(el) {
var vd = $(this).find('.J-media-item').text().replace(/\s/g,'');
var vid = $(this).attr('data-media-id');
article.videos.push({
vTitle: vd,
vid: vid
})
});
console.log(article)
// courseData.push(article)
});
}
http.get(url,function (res) {
var html = ''
res.on('data',function(data){
html += data;
})
res.on('end',function(){
// console.log(html);
handlData(html);
})
}).on('error',function(){
console.info('get data err!');
})