Yonezu.js 2.37 KB
var request = require('request');
var cheerio = require('cheerio');

function crawl_yonezu(url, callback){
    let json_album = [], json_year = [], json_track = [];
    let yonezu = [];
    var track;
    var year;
    request(url, function (err, res, body) {
        const $ = cheerio.load(body);
        var kenshi = $('body > div.content-wrapper > article > div.wiki-content.clearfix > div')
        var index;
        var album_num = 0;
        for (var i = 1; i <= $('.toc-item').length; i++) {
            album = $(kenshi).find('#toc > div > div:nth-child(5) > div:nth-child(5) > span:nth-child(' + i + ')').text() //앨범 정보 가져오기
            if (album != '') {
                album_num++
                album = album_num+'. '+album.substr(14)
                json_album.push(album);
            }

            index = 40+i*2;
            var album_track = '';
            var track_num = 0;
            var check = false;
            if($('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child('+index+')') != ''){
                $('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child('+index+') > div > table > tbody').find('tr').each(function (index, ele) {
                    track = $(this).children().eq(1).text();
                    if(check == true){
                        track_num++;
                        album_track += track_num+'. '+track + '\n';
                    }
                    if(track == '곡명')
                        check = true;
                });
                json_track.push(album_track);
    
                if(index == 44){
                    year = $('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child(44) > div:nth-child(3) > table > tbody > tr:nth-child(2) > td:nth-child(1)').children().eq(0).text();
                    year = year.substr(0,12)
                }
                else{
                    year = $('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child('+index+') > div > table > tbody > tr:nth-child(3)').children().eq(0).text();
                    year = year.substr(0,12)
                }
                json_year.push(year)
            }
        }      

        yonezu.push({ json_album, json_year, json_track});
        callback(yonezu);
    });
}

exports.crawl_yonezu = crawl_yonezu;