티스토리 뷰
nodejs 의 cheerio 모듈을 이용하여
네이버의 IT 뉴스 홈페이지를 파싱하는 소스이다.
한글파싱을 위하여 iconv-list 모듈을 이용하였다.
var request = require("request"); var cheerio = require('cheerio'); var iconv = require('iconv-lite'); var requestOptions = { method: "GET" ,uri: "http://news.naver.com/main/list.nhn?mode=LS2D&mid=shm&sid1=105&sid2=731" ,headers: { "User-Agent": "Mozilla/5.0" } ,encoding: null }; request(requestOptions, function(error, response, body) { iconv.extendNodeEncodings(); var strContents = new Buffer(body); //console.log(strContents.toString('euckr')); // 한글로 바디 전체를 출력 $ = cheerio.load(strContents.toString('euckr')); //console.log($('.list_body.newsflash_body').html()); // 기사 목록을 출력 result = [] $('.list_body.newsflash_body').find('li').each(function(index, ele) { var dt1 = $(this).find('dt').eq(0); console.log(dt1.find('a').attr('href')); console.log(dt1.find('img').attr('src')); console.log("***"); // 이미지가 없는 경우도 있기 때문에, 이를 확인하여 처리 var dt2; if($(this).find(".photo").length == 1) { dt2 = $(this).find('dt').eq(1); } else { dt2 = dt1; } //var dt2 = $(this).find('dt').eq(1); console.log(dt2.find('a').attr('href')); console.log(dt2.find('a').text()); console.log("***"); var dd = $(this).find('dd').eq(0); console.log(dd.text()); console.log(dd.find(".writing").text()); console.log(dd.find(".date").text()); // 기사의 정보를 result 에 맵으로 입력 1 result.push({'url' : dt1.find('a').attr('href'), 'img' : dt1.find('img').attr('src'), 'title' : dt2.find('a').text().trim(), 'content' : dd.text().trim(), 'writing' : dd.find(".writing").text().trim(), 'date' : dd.find(".date").text().trim()}); console.log(new Buffer($(this).html()).toString('euckr')); console.log("---------------------------"); }); console.log(result); });
npm cheerio - https://www.npmjs.com/package/cheerio
네이버 IT 뉴스 - http://news.naver.com/main/list.nhn?mode=LS2D&mid=shm&sid1=105&sid2=731
반응형
'프레임워크 > [JS] node.js' 카테고리의 다른 글
[nodejs / request] request 모듈을 이용하여 이미지 파일 다운로드 하기 (0) | 2016.04.11 |
---|---|
[nodejs] zip 파일 생성하기 (1) | 2016.04.11 |
[nodejs] 한글이 깨지는 현상 수정하기 (0) | 2016.03.30 |
[nodejs][express] express 설치 및 기본 프로젝트 생성하기 (0) | 2016.03.30 |
[jade] node.js용 html 템플릿 엔진 (0) | 2016.03.10 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Tez
- Linux
- java
- AWS
- 하둡
- oozie
- error
- hbase
- airflow
- 다이나믹
- 하이브
- ubuntu
- yarn
- 오류
- bash
- SQL
- Hadoop
- build
- 파이썬
- HDFS
- 백준
- Python
- HIVE
- emr
- 알고리즘
- 정올
- mysql
- S3
- nodejs
- SPARK
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함