Posted by admin July 31st 2018 at 14:07

const puppeteer = require('puppeteer');
const cheerio = require('cheerio');

(async ()=> {
const browser = await puppeteer.launch();
const page = await browser.newPage();

await page.goto('https://www.melon.com/chart/index.htm#params%5Bidx%5D=1', {timeout:5000});

const _body = await page.evaluate(() => {
return document.querySelector('body').innerHTML;
});

const $ = await cheerio.load(_body,{ decodeEntities: false });

var date = await $('span.yyyymmdd > span.year').html();
var time = await $('span.hhmm > span.hour').html();
console.log(date + ' || ' + time);

var rslts = await $('tbody tr');

await rslts.each(function(ii, element){
var rank = $(element).find($('td div span')).html();
if(isNaN(rank)){ return ; }
var title = $(element).find($('td div div.wrap_song_info div.rank01 span a')).html();
var artist = $(element).find($('td div div.wrap_song_info div.rank02 a')).html();
console.log('rank : ' + rank + ' || title : ' + title + ' || artist : ' + artist);
});

await browser.close();

})();