Mot.js 1.83 KB
var request = require('request');
var cheerio = require('cheerio');

function crawl_mot(callback){
    var url = 'https://namu.wiki/w/%EB%AA%BB(%EB%B0%B4%EB%93%9C)';
    let json_album = [], json_year = [], json_track = [];
    let Mot = [];
    var track;
    json_year.push('2004. 06. 18');
    json_year.push('2007. 05. 22');
    json_year.push('2016. 02. 19');

    request(url, function (err, res, body) {
        const $ = cheerio.load(body);
        var index = 12;
        var album_num = 0;
        for(var i = 1; i<=3; i++){
            index += 2;
            if(index == 18)
                index += 6;
            var title_index = 0;
            var track_cnt = 0;
            var album_track = '';
            $('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child('+index+') > div > table > tbody').find('tr').each(function () {
                title_index++;
                if (title_index == 1) {
                    album_num++;
                    album = $('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child('+index+') > div > table > tbody > tr:nth-child(1)').text()
                    album = album_num + '. ' + album;
                }
                else if (title_index >= 3) {
                    track_cnt++;
                    track = $('body > div.content-wrapper > article > div.wiki-content.clearfix > div > div:nth-child('+index+') > div > table > tbody > tr:nth-child(' + title_index + ') > td:nth-child(2)').text();
                    track = track_cnt + '. ' + track;
                    album_track += track + '\n';
                }
            });
            json_album.push(album);
            json_track.push(album_track);
        }
    
        Mot.push({ json_album, json_year, json_track});
        callback(Mot);
    });
}

exports.crawl_mot = crawl_mot;