node.js 에서 HTML을 파싱하기 위해서 cheerio를 이용한다.
cheerio의 설명은 위와 같이 jQuery를 서버사이드에 맞게 수정한 것이다.
따라서 jQuery와 유사하게 동작하므로 기존에 사용하던 사람은 좀더 편안하게 사용할 수 있다.
cheerio - https://github.com/cheeriojs/cheerio
아래의 예제는 에어코리아 사이트의 미세먼지 예보를 파싱한 것이다.
http://www.airkorea.or.kr/dustForecast
var request = require("request");
var cheerio = require('cheerio')
var requestOptions = { method: "GET"
,uri: "http://www.airkorea.or.kr/dustForecast"
,headers: { "User-Agent": "Mozilla/5.0" }
};
request(requestOptions, function(error, response, body) {
// 웹사이트 로드
$ = cheerio.load(body);
info_list = []
colspan_index = []
// 미세먼지 예보 정보를 가지고있는 첫번째 테이블을 확인
$('.table_04.mb10').first().find('tr').each(function (index, elem) {
// 지역 구분 입력
if(index == 0){
$(this).find('th').each(function (index, elem) {
info_list.push({ "name" : $(this).text().trim() })
if($(this).attr('colspan') == '2') {
colspan_index.push(index);
info_list.push({ "name" : $(this).text().trim() })
}
});
}
// 경기, 강원 구분 입력
if(index == 1){
$(this).find('th').each(function (index, elem) {
info = info_list[colspan_index[0] + index];
info["name"] = info["name"] + " " + $(this).text();
info_list[colspan_index[0] + index] = info;
});
}
// 미세면지, PM10, PM2.5 데이터 입력
if(index >= 2){
// text 만 구분하여 입력한다.
info_str = $(this).text().split("\n");
info_index = 0
info_key = ""
for(index in info_str) {
info = info_str[index].trim()
if( info != "" ) {
if (info_index == 0){
info_key = info
} else {
dust_info = info_list[info_index];
dust_info[info_key] = info
}
info_index++;
}
}
}
});
// 결과 출력
for (index in info_list) {
console.log(info_list[index])
}
}); 반응형
'프레임워크 > [JS] node.js' 카테고리의 다른 글
| node.js의 LTS 버전과 Stable 버전의 차이 (1) | 2016.01.27 |
|---|---|
| node.js 의 장점과 단점 (0) | 2016.01.22 |
| request 모듈을 이용하여 HTML 가져오기 (1) | 2016.01.11 |
| node.js 란? (0) | 2015.12.24 |
| [설정] npm 프록시 설정 (1) | 2015.12.21 |