[์นด์นด์˜ค๋ทฐ] ๋ณด๋“œ ์ž๋™ ๋ฐœํ–‰ 1๋‹จ๊ณ„. ๊ตฌ๊ธ€ ์•ฑ ์Šคํฌ๋ฆฝํŠธ๋กœ ๋„ค์ด๋ฒ„ ๋‰ด์Šค, ๋‹ค์Œ ์›น๋ฌธ์„œ ๊ฐ€์ ธ์˜ค๊ธฐ(OPEN API)
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋””์ง€ํ„ธ ๊ฟ€ํŒ

[์นด์นด์˜ค๋ทฐ] ๋ณด๋“œ ์ž๋™ ๋ฐœํ–‰ 1๋‹จ๊ณ„. ๊ตฌ๊ธ€ ์•ฑ ์Šคํฌ๋ฆฝํŠธ๋กœ ๋„ค์ด๋ฒ„ ๋‰ด์Šค, ๋‹ค์Œ ์›น๋ฌธ์„œ ๊ฐ€์ ธ์˜ค๊ธฐ(OPEN API)

by ์ผ์ƒ์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค 2022. 3. 17.

์นด์นด์˜ค๋ทฐ ์ž๋™๋ฐœํ–‰์„ ํ•˜๋ ค๋‹ค ๋ณด๋‹ˆ ์–ด๋–ค ์ฝ˜ํ…์ธ ๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ• ์ง€ ๊ณ ๋ฏผํ–ˆ๋‹ค. ๊ธฐ์กด๊นŒ์ง€ ์ˆ˜๋™์œผ๋กœ ํ•˜๋˜ ๋ณด๋“œ ๋ฐœํ–‰์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ž๋™ ๋ฐœํ–‰์„ ์ƒ๊ฐํ•ด๋ณด๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

1.  ์ฝ˜ํ…์ธ  : ๋„ค์ด๋ฒ„ , ๋‹ค์Œ ๋‰ด์Šค, ์ž‘์„ฑํ•œ ํ‹ฐ์Šคํ† ๋ฆฌ, ์ž‘์„ฑํ•œ ๋ธ”๋กœ๊ทธ ์ฝ˜ํ…์ธ 
2. Tool :  ๋ˆ„๊ตฌ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ๊ธ€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ๋ฐ ๊ตฌ๊ธ€ ์•ฑ์Šคํฌ๋ฆฝํŠธ

๊ฒฐ๊ณผ๋ฌผ 


๋„ค์ด๋ฒ„ ๋‰ด์Šค ๊ฐ€์ ธ์˜ค๊ธฐ

  • ๊ตฌ๊ธ€ ์Šคํ”„๋ ˆ๋“œ ์‹œํŠธ๋ฅผ ์—ด๊ณ , app script๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

 

  • ์•„๋ž˜ ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ €์žฅํ•˜๊ณ  ์‹คํ–‰์‹œํ‚จ๋‹ค.
function naverNews(searchWord) {

  var client_key = "๊ฐœ์ธ KEY";
  var client_secret = "๊ฐœ์ธ SECRET";

  var url = "https://openapi.naver.com/v1/search/news.json?query=" + searchWord

  var response = UrlFetchApp.fetch(url,{'method' : 'GET',
        'headers': {                    
          'X-Naver-Client-Id' : client_key,
          'X-Naver-Client-Secret' : client_secret
        }
    }); 
  
  var resData = response.getContentText();
  resData = JSON.parse(resData);

  var arrTitle = new Array();
  var arrDescription = new Array();
  var arrLink = new Array();
  var arrDate = new Array();

  console.log(resData);

  for(var i = 0 ; i < 1 ; i ++ )
  {   
    arrTitle.push(replaceTitle(resData.items[i].title));
    arrLink.push(resData.items[i].link);
    arrDescription.push(replaceTitle(resData.items[i].description));
    arrDate.push(resData.items[i].pubDate);
  }

  var arrInfo = new Array();
  arrInfo.push(arrTitle);
  arrInfo.push(arrLink);
  arrInfo.push(arrDescription);
  arrInfo.push(arrDate);

//์Šคํ”„๋ ˆ๋“œ์‹œํŠธ์— ์„ธ๋กœ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ...
  var transposed = Array.from({ length: arrTitle.length }, () => new Array().fill(0)) 

  for (var o=0; o< arrInfo.length; o++) {
     for (var v=0; v<arrInfo[0].length; v++) { 
        [transposed[v][o]] = [arrInfo[o][v]];
    }
  }

  return transposed;
}

 

  • ๊ตฌ๊ธ€ ์Šคํ”„๋ ˆ๋“œ ์‹œํŠธ์—์„œ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. C1์€ ์…€ ์œ„์น˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ๊ด€์‹ฌ ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค (์˜ˆ : ์—ฐ์˜ˆ)

 


 

์นด์นด์˜ค ์›น ๋ฌธ์„œ ๊ฐ€์ ธ์˜ค๊ธฐ

  • ์ƒ์„ฑํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ > ์•ฑ ์„ค์ • > ์•ฑ ํ‚ค 

  • ์•„๋ž˜ ์†Œ์Šค๋ฅผ ๋„ค์ด๋ฒ„ ์œ„ ์†Œ์Šค์™€ ๊ฐ™์ด ์ €์žฅํ•˜์—ฌ ์‹คํ–‰ํ•œ๋‹ค
function DaumNews(searchWord) {

  var REST_API_KEY = '๊ฐœ์ธ KEY';

  for ( var p=1; p<5 ; p++)
  {
    var url = "https://dapi.kakao.com/v2/search/web?query=" + searchWord + '&page='+ p +'&size=10&sort=recency'

    var response = UrlFetchApp.fetch(url,{'method' : 'GET',
          'headers': {                    
            'Authorization' : 'KakaoAK ' + REST_API_KEY
          }
      }); 
    
    var resData = response.getContentText();
    resData = JSON.parse(resData);

    var arrTitle = new Array();
    var arrLink = new Array();
    var arrContent = new Array();
    var arrDate = new Array();

    for(var i = 0 ; i < 1 ; i ++ ){
      if(resData.documents[i].url.indexOf("wiki") == -1 && resData.documents[i].url.indexOf("html") == -1)
      {     
            arrTitle.push(replaceTitle(resData.documents[i].title));
            arrLink.push(resData.documents[i].url);
            arrContent.push(replaceTitle(resData.documents[i].contents));            
            arrDate.push(resData.documents[i].datetime);
      }
    }

    var arrInfo = new Array();
    arrInfo.push(arrTitle);
    arrInfo.push(arrLink);
    arrInfo.push(arrContent);
    arrInfo.push(arrDate);

  }

  var transposed = Array.from({ length: arrTitle.length }, () => new Array().fill(0)) 


  for (var o=0; o< arrInfo.length; o++) {
     for (var v=0; v<arrInfo[0].length; v++) { 
        [transposed[v][o]] = [arrInfo[o][v]];
    }
  }

  return transposed;
}


function replaceTitle(title){

  var tempTitle = "";
  
  tempTitle = title.replaceAll("&quot;","")
  tempTitle = tempTitle.replaceAll("&#34;","");
  tempTitle = tempTitle.replaceAll("&amp;","")
  tempTitle = tempTitle.replaceAll("<b>","");
  tempTitle = tempTitle.replaceAll("</b>","")
  tempTitle = tempTitle.replaceAll("&#39;","");
  tempTitle = tempTitle.replaceAll("&lt;","");
  tempTitle = tempTitle.replaceAll("&gt;","");
  tempTitle = tempTitle.replaceAll("  "," ");

   
  return tempTitle;
}

 

  • ๊ตฌ๊ธ€ ์Šคํ”„๋ ˆ๋“œ ์‹œํŠธ์—์„œ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

๋„ค์ด๋ฒ„๋‚˜ ๋‹ค์Œ์ด๋‚˜ ์ง„์งœ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ ์ด ๋ถ€๋ถ„์— ํ•ด๋‹น ๋œ๋‹ค. ๋‹จ ๋ช‡ ์ค„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์— ์›น๋ฌธ์„œ๋Š” ์ถ”ํ›„ ์ตœ์‹  ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊นŒ์ง€ ์ถ”๊ฐ€ํ•ด๋ณด๊ฒ ๋‹ค.

 var url = "https://openapi.naver.com/v1/search/news.json?query=" + searchWord

  var response = UrlFetchApp.fetch(url,{'method' : 'GET',
        'headers': {                    
          'X-Naver-Client-Id' : client_key,
          'X-Naver-Client-Secret' : client_secret
        }
    }); 
  
  var resData = response.getContentText();
  resData = JSON.parse(resData);

 

ํ‹ฐ์Šคํ† ๋ฆฌ ๊ฒŒ์‹œ๊ธ€ ํƒ€์ดํ‹€๊ณผ ๋งํฌ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ํŽธ ๐Ÿ‘‡ 

 

 

[์นด์นด์˜ค๋ทฐ] ๋ณด๋“œ ์ž๋™ ๋ฐœํ–‰ 2๋‹จ๊ณ„. ๊ตฌ๊ธ€ ์•ฑ ์Šคํฌ๋ฆฝํŠธ๋กœ ๋ณธ์ธ ํ‹ฐ์Šคํ† ๋ฆฌ ํฌ์ŠคํŒ… ๊ธ€ ๊ฐ€์ ธ์˜ค๊ธฐ(OPEN API)

๊ตฌ๊ธ€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ค„ ์•„๋Š” ๋ถ„์ด๋ฉด ๋ˆ„๊ตฌ๋‚˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! 1๋‹จ๊ณ„. ํ‹ฐ์Šคํ† ๋ฆฌ API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์•ฑ์„ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. ํ‹ฐ์Šคํ† ๋ฆฌ API ์•ฑ ๋“ฑ๋ก ์‚ฌ์ดํŠธ : https://www.tistory.com/guide/api/manage/regist

odaily.tistory.com

 

๋Œ“๊ธ€