[Javascript][복습][크롤링][오늘의집]

Dev_Honey·2022년 7월 20일
0

복습

목록 보기
1/1

Javascript로 만드는 크롤링 혼자 해보기 !

오늘의 집에서 자료를 긁어보자 !

crawler.js

import  puppeteer  from "puppeteer-core";
//puppeteer 다운 받은 것을 임포트 시킨다.
import os from 'os'
import fs from 'fs'

const macUrl = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
// Chrome browser로 실행시킬 것이기에 chrome.exe가 있는 경로를 입력
const whidowsUrl = 'C:/Program Files (x86)/Google/Chrome/Application/Chrome.exe'
const currentOs = os.type()
const launchConfig = {
  headless: false,
  defaultViewport: null,
  ignoreDefaultArgs: ['--disable-extensions'],
  args: [ '--no-sandbox', '--disable-setuid-sandbox', '--disable-notifications', '--disable-extensions'],
  executablePath: currentOs == 'Darwin' ? macUrl : whidowsUrl
}

//전역변수
let browser = null
let page = null
let pageLength = 0
const pageSelector = ""

//실행함수
const launch = async function(){
    browser = await puppeteer.launch(launchConfig);
    const pages = await browser.pages();
    console.log(pages.length)
    page = pages[0]
}


export {
  launch
}

index.js

import { launch } from './modules/crawler.js'

async function main() {
  await launch()
}
main()

진행상황

  • 첫번 째 오류 !! 모듈을 찾을 수 없다고 한다.

  • (해결)

  1. yarn init
  2. yarn add puppeteer-core 로 puppeteer-core 설치
  3. package.json의 Dependency에 puppeteer가 추가된 것을 볼 수 있다.
    4."type" : "module"을 추가 한다.
  4. module을 못찾길래 터미널에서 npm install을 했다.
  5. 그리고 다시 node index.js를 실행하니 브라우져 실행이 되었다.

오늘의 집 브라우져까지 띄웠다.

profile
자습서 같은 공부 블로그 만들기!

0개의 댓글