Reol.js
3.49 KB
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
59
60
61
62
63
64
65
//Reol 나무위키 앨범 발매년도 트랙 크롤링
var request = require('request');
var cheerio = require('cheerio');
function crawl_Reol(url,callback){
request(url, function (err, res, body) {
const $ = cheerio.load(body);
var Reol = new Array();
var album, year
var album_track = ''
var json_album = new Array(), json_year = new Array(), json_track = new Array();
$('body > div.content-wrapper > article > div.wiki-content.clearfix > div').each(function (index, ele) {
for (var i = 1; i <= $('.toc-item').length; i++) {
album = $(this).find('#toc > div > div:nth-child(2) > span:nth-child(' + i + ')').text() //앨범 정보 가져오기
if (album != '') {
json_album.push(album.substr(2));
}
var index = 7 + (i * 2); //트랙, 연도 가져오기 인덱스 변수
if (index != 25) { //규칙성이 어긋나는 부분을 예외처리를 위해 if문
//year 가져오기
year = $('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child(' + index + ') > div:nth-child(3) > table > tbody > tr:nth-child(1) > td:nth-child(2)').text();
//track 가져오기
var track_num = 0;
$('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child(' + index + ') > div:nth-child(3) > table > tbody').find('tr').each(function (index, ele) {
var track = $(this).children().eq(1).text();
if (track != '1' && track != '곡명' && track[4] != '년'){
if(track[track.length-1] == 'M')
track = track.substr(0,track.length-1);
if(track[track.length-1] == ']')
track = track.substr(0,track.length-2);
track_num++;
album_track += track_num+'. '+track + '\n';
}
});
}
else {
year = $('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child(' + index + ') > div:nth-child(4) > table > tbody > tr:nth-child(1) > td:nth-child(2)').text();
var track_num = 0;
$('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child(' + index + ') > div:nth-child(4) > table > tbody').find('tr').each(function (index, ele) {
var track = $(this).children().eq(1).text();
if (track != '1' && track != '곡명' && track[4] != '년'){
if(track[track.length-1] == 'M')
track = track.substr(0,track.length-1);
if(track[track.length-1] == ']')
track = track.substr(0,track.length-2);
track_num++;
album_track += track_num+'. '+track + '\n';
}
});
}
if (year != '') {
json_year.push(year);
json_track.push(album_track);
}
album_track = [];
}
});
Reol.push({ json_album, json_year, json_track });
callback(Reol);
});
}
exports.crawl_Reol = crawl_Reol;