인프런 커뮤니티 질문&답변

집중하기 매우 어려움님의 프로필 이미지

작성한 질문수

Node.js로 웹 크롤링하기

4-2. 인피니트 스크롤 태그 분석하기

선장님 도와주십쇼!

해결된 질문

22.04.18 21:16 작성

·

126

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();

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

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

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

콘솔 태그 결과입니다.

 

 

답변 1

1

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

2022. 04. 18. 21:42

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

집중하기 매우 어려움님의 프로필 이미지

2022. 04. 18. 22:10

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