Reol.js 3.49 KB
//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;