Skip to content

CHICPRO

  • Life Log
  • Cycling Log
  • Photo Log
    • Portrait
    • Landscape
    • Flower
    • Etc
  • Coding Log
  • Information

puppeteer, cheerio 를 이용한 웹 크롤링

2019-09-06 by 편리

puppeteer, cheerio 를 이용해 네이버 쇼핑의 특정 카테고리 상품명 가격을 크롤링하여 콘솔에 표시하는 것을 테스트했다. nodejs 와 puppeteer, cheerio 는 미리 설치되어 있어야 한다. cheerio 는 jQuery 문법을 그대로 사용할 수 있어 상당히 편하다.

네이버 쇼핑의 상품정보를 크롤링하는 경우에는 puppeteer 모듈을 사용하지 않고도 가능하다. 하지만 최근의 vue.js 등을 사용한 웹페이지는 크롤링이 가능하지 않을 수 있기 때문에 puppeteer 를 사용한다.

테스트에 사용한 코드는 아래와 같다.

const puppeteer = require('puppeteer');
const $ = require('cheerio');

function sleep(ms){
  return new Promise(resolve => {
      setTimeout(resolve, ms)
  });
}

async function printConsole(content) {
  const body = $.load(content);
  const anchorsSelector = '#_search_list ul.goods_list li div.info';

  var anchors = [];

  var title, price, url, elA;

  body(anchorsSelector).each(function() {
    anchors.push($(this));
  });

  if (anchors.length > 0) {
    var i = 0;

    for (const el of anchors) {
      elA = el.children("a.tit");

      title = elA.text().trim();
      url = elA.attr("href");
      price = el.find(".price em span._price_reload").text().trim();

      //console.log(title + "(" + price + ")" + " - " + url);
      console.log(title + "(" + price + ")");

      i++;

      if (i < anchors.length)
        await sleep(1000);
    }
  }
}

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto("https://search.shopping.naver.com/search/category.nhn?pagingIndex=1&pagingSize=40&viewType=list&sort=rel&cat_id=50000089&frm=NVSHCAT", { waitUntil : "networkidle2" });

  const content = await page.content();

  await printConsole(content);

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

위 코드를 실행한 결과는 아래와 같다.

한성컴퓨터 TFG AX9466(783,710)
프리플로우 GAMING i5-9400F RGB + 외장형 그래픽카드(645,000)
삼성전자 오디세이 DM800V7A-A716A(1,762,900)
삼성전자 DM500S8A-A24BA(557,200)
대한컴퓨터 게이밍 조립컴퓨터 배틀그라운드 오버워치 배그 GTX1660Ti 사무용 견적(498,000)
게이밍 조립컴퓨터 배틀그라운드 오버워치 배그 GTX1660Ti 라이젠 3600 3700 사무용 본체 견적(699,000)
애플 아이맥 27형 2019년형 (MRQY2KH/A)(1,942,340)
LG전자 24V570-GR32K(719,000)

Post navigation

Previous Post:

headless-chrome-crawler 사용해보기

Next Post:

[PHP] 간단한 달력 소스

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • SK 세븐모바일 유심 셀프교체
  • php 배열 연산에서 + 와 array_merge 의 차이
  • pcntl_fork 를 이용한 다중 프로세스 실행
  • 아이폰 단축어를 이용하여 주중 공휴일엔 알람 울리지 않게 하기
  • 구글 캘린더 전체일정 재동기화
  • OpenLiteSpeed 웹서버에 HTTP 인증 적용
  • OpenLiteSpeed 웹어드민 도메인 연결
  • WireGuard를 이용한 VPN 환경 구축
  • Ubuntu 22.04 서버에 OpenLiteSpeed 웹서버 세팅
  • 맥 vim 세팅

Recent Comments

  • 편리 on 업무관리용 그누보드 게시판 스킨
  • 임종섭 on 업무관리용 그누보드 게시판 스킨
  • 캐논 5D 펌웨어 | Dslr 펌웨어 업그레이드 방법 82 개의 베스트 답변 on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • Top 5 캐논 5D 펌웨어 Top 89 Best Answers on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • 편리 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 임팀장 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 편리 on Notepad++ NppFTP 플러그인 수동 설치
  • paul-j on Notepad++ NppFTP 플러그인 수동 설치
  • YS on Windows 10 iCloud 사진 저장 폴더 변경
  • 편리 on Docker를 이용한 Centos7 + httpd + php 5.4 개발환경 구축

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2025 CHICPRO | Built using WordPress and SuperbThemes