반응형

기상청의 RSS를 파싱에 'cheerio-httpcli' 를 사용하여 뿌려주는 방법

 

다운로드 : weather.js

 

RSS URL : http://web.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=109

 

 

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
// 기상청 기상예보 RSS(cheerio이용) for Node.js
 
// 기상 RSS
var RSS = "http://web.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=109";
 
// 모듈 로드
var client = require('cheerio-httpcli');
 
// RSS 다운로드
client.fetch(RSS, {}, function(err, $, res) {
  if (err) { 
    console.log("error"); return
  }
 
  var city = $("location:nth-child(1) > city").text();
 
  // 필요한 항목을 추출해서 표시
  $("location:nth-child(1) > data").each(function(idx) {
 
    var tmEf = $(this).find('tmEf').text();
    var wf = $(this).find('wf').text();
    var tmn = $(this).find('tmn').text();
    var tmx = $(this).find('tmx').text();
    
    console.log(city + " " + tmEf + " " + wf + " " + tmn +"~" + tmx);
  });
});
 
cs

 

 

결과 

 

 

 

반응형

'프로그래밍 > Node.js' 카테고리의 다른 글

CasperJS 설치  (0) 2017.04.07
PhantomJS 설치  (0) 2017.04.07
HTML 파일에서 링크 추출  (0) 2017.03.27
node.js 특정 페이지 출력하는 코드  (0) 2017.03.27
윈도우에서 'Node.js'와 'NPM'을 설치하기  (0) 2017.03.25
반응형

명령어 프롬프트(cmd)에서

 

npm init
npm install cheerio-httpcli


인스톨을 한 후에

 

아래와 같은 예제 입력후 파일 저장 (ex. download.js)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var client = require('cheerio-httpcli');
 
var url = "http://nickoo.tistory.com";
var param = {};
 
client.fetch(url, param, function(err, $, res){
  if(err){console.log("error:", err); return;}
 
  var body = $.html();
  console.log(body);
 
});
 
 
 
cs

 

 

이렇게 한 후에 node download.js 를 명령어 프롬프트에 입력하면 HTML문서를 화면에 출력합니다.

 

 

반응형

'프로그래밍 > Node.js' 카테고리의 다른 글

CasperJS 설치  (0) 2017.04.07
PhantomJS 설치  (0) 2017.04.07
XML/RSS 파싱에 cheerio-httpcli 이용  (0) 2017.04.06
HTML 파일에서 링크 추출  (0) 2017.03.27
윈도우에서 'Node.js'와 'NPM'을 설치하기  (0) 2017.03.25

+ Recent posts