app.js 1.94 KB
const request = require('request');
const cheerio = require('cheerio');
const puppeteer = require('puppeteer');
const {Builder,Key,until} = require('selenium-webdriver');  //모듈 불러오기
var webdriver = require('selenium-webdriver');
var By = webdriver.By;
const chrome = require('selenium-webdriver/chrome');//크롬 사용시 


let booking_url = "https://megabox.co.kr/booking";
const rate_url = "https://www.megabox.co.kr/movie";
// var booking_options  = { encoding: "utf-8", method: "GET", uri: booking_url};

let r =0;
let movie_data = [];


(async () => {

    const driver = new webdriver.Builder().forBrowser('chrome').build();
    driver.get(booking_url);
    driver.switchTo().frame(0)//frameBokdMBooking 프레임 가져옴
    
    let list = await driver.wait(until.elementsLocated(By.css('#mCSB_1_container>ul>li>.btn')));
    r = 0;
    for (item of list) {
        //Using getAttribute to get the data
        movie_data[r++] = {
            'rank' : r,
            'title' :  await item.getAttribute("movie-nm"),
            'movie_num':await item.getAttribute("movie-no"),
            'running':await item.getAttribute("form-at"),
        }
    }
    driver.close();

    r = 0;
    const browser = await puppeteer.launch({
        headless: true
      }); 
    const page = await browser.newPage();
    await page.goto(rate_url);
    const content = await page.content();
      
    const $ = cheerio.load(content);
    const $rate_lists = $("ol.list>li");
    $rate_lists.each((index, list) => {
        const name = $(list).find('div.tit-area > p.tit').attr('title');
        const rate = $(list).find('div.rate-date > span.rate').text();
        
        if(movie_data[r].title === name){
            movie_data[r]['rate'] = rate;
        }else{
            movie_data[r]['rate'] = '예매율 0.0%';
        }
    });
    r = 0;
    for(i of movie_data){
        console.log(i);
    }

    browser.close();
})();

// (async () => {

   
// })();