강의

멘토링

커뮤니티

Inflearn Community Q&A

thelapssql3213's profile image
thelapssql3213

asked

Web Crawling with Node.js

4-2. Analyzing the Infinite Scroll Tag

선장님 도와주십쇼!

Resolved

Written on

·

167

0

import puppeteer from "puppeteer";
import axios from "axios";
import fs from "fs";

const botAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36";

const crawlerBot = async () => {
  try {
    const browser = await puppeteer.launch({
      headless: false,
      args: ["--window-size=1920,1080"],
    });
    const page = await browser.newPage();
    await page.setUserAgent(botAgent);
    await page.goto("https://unsplash.com");
    await page.waitFor(2000);
    const result = await page.evaluate(() => {
      const imageArray = [];
      const image = document.querySelectorAll(".ripi6 img.YVj9w");
      if (image.length) {
        image.forEach((e) => {
          imageArray.push(e);
        });
      }
      return imageArray;
    });
    console.log(result);
    await page.close();
    await browser.close();
  } catch (err) {
    console.error(err);
  }
}

crawlerBot();

강의에서 빈값이 들어있는 이유랑 연관이 있는 거 같은데

태그를 찍어봤더니 전부 빈 객체가 들어있습니다.(다른 태그들도 빈 객체) 

언스플래쉬 사이트가 철통보안을 쳐놓은 걸까요?

콘솔 태그 결과입니다.

 

 

웹-크롤링nodejspuppeteerjavascript

Answer 1

1

zerocho님의 프로필 이미지
zerocho
Instructor

태그 자체는 못 가져옵니다. 태그의 데이터만 뽑아서 가져올 수 있습니다. json형식으로요.

thelapssql3213님의 프로필 이미지
thelapssql3213
Questioner

cheerio처럼 html 해석하는 걸로 착각했네요 감사합니다.

thelapssql3213's profile image
thelapssql3213

asked

Ask a question