everytime_crawling.js 2.76 KB
const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  const et_id = "";
  const et_pw = "";
  const lecture = "";
  await page.goto('https://everytime.kr/login');
  await page.evaluate((id, pw) => {
    document.querySelector("input[name=userid]").value = id;
    document.querySelector("input[name=password]").value = pw;
  }, et_id, et_pw);
  await console.log("로그인 정보 삽입 완료")
  await page.click('#container > form > p.submit > input');
  await page.waitFor(1000);
  await page.goto('https://everytime.kr/lecture');
  await page.evaluate((lec) => {
    document.querySelector("input[name=keyword]").value = lec;
  }, lecture);
  await page.click('#container > form > input.submit');
  await page.waitFor(1000);

  let tempclass = await page.evaluate((sel) => {
    return Array.from(document.getElementsByClassName(sel)).map(node => node.href);
  }, 'lecture');
  console.log(tempclass)


  for (var i in tempclass) {
      console.log(tempclass[i]);
      await page.goto(tempclass[i]);
      await page.waitFor(1000);
      await page.waitForSelector('#container > div.side.head > h2');
      const lecturename = await page.evaluate(() => {
        const anchors1 = Array.from(document.querySelectorAll('#container > div.side.head > h2'));
        return anchors1.map(anchor1 => anchor1.textContent);
      });
      await page.waitForSelector('#container > div.side.head > p:nth-child(3) > span');
      const proname = await page.evaluate(() => {
        const anchors2 = Array.from(document.querySelectorAll('#container > div.side.head > p:nth-child(3) > span'));
        return anchors2.map(anchor2 => anchor2.textContent);
      });
      await page.waitForSelector('#container > div.side.article > div.rating > div.rate > span > span.value');
      const avg_rate = await page.evaluate(() => {
        const anchors3 = Array.from(document.querySelectorAll('#container > div.side.article > div.rating > div.rate > span > span.value'));
        return anchors3.map(anchor3 => anchor3.textContent);
      });
      let review = [];
      if (await page.$('#container > div.side.article > div.articles > article > p.text') !== null){
        await page.waitForSelector('#container > div.side.article > div.articles > article > p.text');
          review =  await page.evaluate(() => {
          const anchors4 = Array.from(document.querySelectorAll('#container > div.side.article > div.articles > article > p.text'));
          return anchors4.map(anchor4 => anchor4.textContent);
        });
      }
      //콘솔에 출력한다.
      console.log(lecturename.join('\n'));
      console.log(proname.join('\n'));
      console.log(avg_rate.join('\n'));
      console.log(review); 
  }

  await browser.close();
})();