cheerio와 Puppeteer는 웹 스크래핑과 웹 자동화를 위한 JavaScript 라이브러리입니다. 하지만 각각 다른 목적과 특징을 가지고 있습니다. 아래에서 각 라이브러리의 사용법과 장단점을 비교해보겠습니다.
cheerio는 주로 서버 측에서 HTML 파싱과 조작을 위해 사용됩니다. jQuery와 유사한 API를 제공하여 HTML 문서를 조작하고 데이터를 추출하는 데에 편리합니다
const cheerio = require('cheerio');
const html = '<h1>Hello, Cheerio!</h1>';
const $ = cheerio.load(html);
const text = $('h1').text();
console.log(text); // 출력: "Hello, Cheerio!"
Puppeteer는 Chrome 브라우저를 제어하여 웹 페이지를 스크랩하고 자동화합니다. 동적으로 로드되는 페이지나 JavaScript 실행이 필요한 작업을 수행할 수 있습니다.
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.example.com');
const title = await page.title();
console.log(title); // 출력: "Example Domain"
await browser.close();
})();
비교적 정적인 페이지 스크랩이나 파싱에는 Cheerio가 유용하며, 동적 페이지나 SPA에서는 Puppeteer가 적합합니다. 선택은 사용하고자 하는 작업의 종류와 요구사항에 따라 달라질 수 있습니다.
CommonJS module 와 ES module (2) | 2023.11.01 |
---|---|
innerHTML, innerText, textContent 비교 (0) | 2023.08.27 |
puffeteer - $$eval(), $eval() (0) | 2023.08.26 |
FormData 객체 활용하기 (1) | 2023.07.08 |
VS Code 코드 추천 확장 프로그램 (extensions) (0) | 2023.07.08 |