- javascript를 활용해서 웹사이트를 크롤링 합니다.
const axios = require('axios');
const cheerio = require('cheerio');
export const getHtml = async () => {
try {
return await axios.get('https://naver.com');
} catch (error) {
console.error(error);
}
};
export const get = async () => {
const html = await getHtml();
let ulList = [];
const $ = cheerio.load(html.data);
const $bodyList = $('tbody').children('tr');
$bodyList.each(function(i, elem) {
ulList[i] = {
title: $(this)
.find('td:nth-child(3)')
.text(),
gender: $(this)
.find('td:nth-child(4)')
.text(),
birthday: $(this)
.find('td:nth-child(5)')
.text(),
character: $(this)
.find('td:nth-child(6)')
.text(),
};
const data = ulList.filter(n => n.title);
return data;
});
return ulList;
};