티스토리 뷰

node.js 웹페이지 크롤링한 결과를 excel 파일로 저장


npm init

npm install --save cheerio-httpcli

npm install --save officegen


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var client = require('cheerio-httpcli');
var officegen = require('officegen');
var xlsx = officegen('xlsx');
var fs = require('fs');
 
var url = "https://search.naver.com/search.naver?where=post&sm=tab_pge&query=%EC%84%B1%ED%98%95%EC%99%B8%EA%B3%BC&st=sim&date_option=0&date_from=&date_to=&dup_remove=1&post_blogurl=&post_blogurl_without=&srchby=all&nso=&ie=utf8&start=1";
var param = {};
 
client.fetch(url, param, function(err, $, res){
  if(err){console.log("error:", err); return;}
 
  var body = $.html();
 
  var re = /성형/g;
  var found = body.match(re);
  //console.log(body);
  //console.log(found);
  console.log(found.length);
 
});
 
 
client.fetch(url, param, function(err, $, res){
  if(err){console.log("error:", err); return;}
 
  var body = $.html();
  var list_dl = $(".sh_blog_passage");
  //console.log(list_dl);
  var list = [];
  for (var i = 0 ; i < list_dl.length ;i++){
    var value = $(list_dl[i]).html();
    console.log(value);
    list.push(value);
 
  }
  exportToExcel(list);
});
 
 
function exportToExcel(list){
  var sheet = xlsx.makeNewSheet();
  sheet.name = "test";
 
  for (var i = 0 ; i < list.length ;i++){
 
    var value = list[i];
    console.log(value);
    sheet.setCell('a' + i+1, value);
  }
 
  var strm = fs.createWriteStream('c:/practice/node_crolling_02/test.xlsx');
  xlsx.generate(strm);
 
}
 
cs



end.



공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함