권은령

update genie music crawling

1 var cheerio = require('cheerio'); 1 var cheerio = require('cheerio');
2 var request = require('request'); 2 var request = require('request');
3 3
4 -var url="http://www.melcon.com/chart/"; 4 +var url="https://www.genie.co.kr/chart/top200";
5 +
5 6
6 request(url, function(error, response, html) 7 request(url, function(error, response, html)
7 { 8 {
8 var $ = cheerio.load(html); 9 var $ = cheerio.load(html);
9 10
10 - var arr=$('.ellipsis rank01'); 11 + var songList=[];
11 - console.log(arr[0].children[0].attribs.title); 12 +
13 + const $bodyList= $('#body-content > div.newest-list > div > table > tbody > tr');
14 + console.log($bodyList.length);
15 +
16 + $bodyList.each(function(i, elem)
17 + {
18 + if(i<20)
19 + {
20 + songList.push(
21 + {
22 + singer: $(this).find("td.info").find("a.artist.ellipsis").text().trim(),
23 + song: $(this).find("td.info").find("a.title.ellipsis").text().trim(),
24 + url: 'https://www.genie.co.kr/detail/songInfo?xgnm='+$(this).attr("songid")
25 + }
26 + );
27 + }
28 + else
29 + {
30 + return;
31 + }
32 + })
33 +
34 + for(var i=0; i<songList.length; i++)
35 + {
36 + console.log(songList[i]);
37 + }
38 +
39 + var randnum=function getRandomInt(min, max)
40 + {
41 + min = Math.ceil(min);
42 + max = Math.floor(max);
43 + return Math.floor(Math.random() * (max - min)) + min; //최댓값은 제외, 최솟값은 포함
44 + }(0, 20);
45 +
46 + console.log(randnum);
47 + var newUrl=songList[randnum].url;
12 48
13 -}) 49 + request(newUrl, function(error, response, html)
50 + {
51 + var $ = cheerio.load(html);
14 52
53 + console.log(songList[randnum].song);
54 + console.log($('#pLyrics > p').text());
55 + });
56 +})
...\ No newline at end of file ...\ No newline at end of file
......