개발/개발 자료

NodeJS 사이트 데이터 크롤링 하기

시원한물냉 2022. 4. 6. 22:50

 

npm install axios cheerio

 

const html = await axios.post('https://naver.com');
const $ = cheerio.load(html.data);

 

이렇게만 설정하면 사이트를 긁어올 준비느 끝났다.

 

const foo = $('div.test');

 

이와같이 작성하면 div 태그중  test 라는 클래스를 가진 속성을 불러온다.

type은 object로 {} 혹은 [] 로 불러와진다.

 

const foo = $('div#test');

이렇게 id를 통해 찾을 수 도 있다.

 

마지막으로,

const foo = $('div.test>div.bar');

이렇게 > 연산자를 통해 하위 컴포넌트를 검색할 수 있다.

div 태그의 test 클래스 하위의 div 태그의 bar 클래스를 찾아오는 코드이다.