function fetchNewsAndSendEmail() {
var keywords = ["골프", "PGA", "LPGA", "KPGA"]; // 키워드 "" 넣어주는 곳
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy.MM.dd");
var results = [];
sheet.clear(); // 기존 데이터 삭제 후 새로 저장
sheet.appendRow(["검색 키워드", "날짜", "제목", "URL"]); // 헤더 추가
for (var i = 0; i < keywords.length; i++) {
var keyword = encodeURIComponent(keywords[i]); // URL 인코딩
var url = "https://search.naver.com/search.naver?where=news&query=" + keyword;
var options = {
method: "get",
muteHttpExceptions: true,
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
};
var response = UrlFetchApp.fetch(url, options);
var html = response.getContentText();
// 뉴스 제목과 URL 추출 (정규식 활용)
var titleRegex = /]+class=["']news_tit["'][^>]+title=["']([^"']+)["'][^>]+href=["']([^"']+)["']/g;
var match;
var count = 0;
while ((match = titleRegex.exec(html)) !== null && count < 1) { // 키워드당 1개 기사만 가져오기
var title = match[1];
var link = match[2];
sheet.appendRow([keywords[i], today, title, link]); // 스프레드시트 저장
results.push(" ");
count++;
}
}
if (results.length > 0) {
sendEmail(results);
}
}
function sendEmail(newsData) {
var recipient = "000여러분아이디@주소"; // 📩 받을 이메일 주소 입력
var subject = "오늘의 골프 뉴스 업데이트";
var body = "";
body += "" + keywords[i] + " " + today + " " + title + " " + link + " 오늘의 골프 뉴스
";
body += "
검색 키워드 | 날짜 | 제목 | URL |
---|