2025年3月15日 星期六

【Google教學】如何用GoogleAppsScript爬取網頁看youtube的所有留言(使用chatgpt的作法)

 


有些人會在youtube影片的留言來抽獎的作法,那我們怎麼去把某一篇影片的留言都抓到google試算表,我們把這個問題拿去問AI(chatgpt),它提供我們程式碼及作法。


1.申請YouTube Data API 來獲取留言

2.查看要查詢的youtube影片ID

3.修改程式碼並執行



程式碼:

function getYouTubeComments() {

  var apiKey = "YOUR_API_KEY";  // 請填入你的 YouTube API Key

  var videoId = "YOUR_VIDEO_ID"; // 影片 ID

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  sheet.clear(); // 清空試算表

  sheet.appendRow(["作者", "留言", "發佈時間 (台灣)", "喜歡數"]); // 更新標題


  var nextPageToken = "";

  var maxResults = 100;


  do {

    var url = "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=" 

              + videoId + "&key=" + apiKey + "&maxResults=" + maxResults + "&pageToken=" + nextPageToken;

    

    var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});

    var json = JSON.parse(response.getContentText());


    if (json.items) {

      json.items.forEach(function(item) {

        var snippet = item.snippet.topLevelComment.snippet;

        var author = snippet.authorDisplayName;

        var comment = snippet.textDisplay;

        var publishedAt = snippet.publishedAt; // 原始 UTC 時間

        var likeCount = snippet.likeCount;


        // 轉換 UTC 時間為台灣時間 (UTC+8)

        var taiwanTime = convertToTaiwanTime(publishedAt);


        sheet.appendRow([author, comment, taiwanTime, likeCount]);

      });

    }


    nextPageToken = json.nextPageToken || "";

  } while (nextPageToken);

}


// 轉換 UTC 時間到台灣時間 (UTC+8)

function convertToTaiwanTime(utcTime) {

  var date = new Date(utcTime);  // 解析 ISO 時間字串

  date.setHours(date.getHours() + 8); // 加 8 小時變成台灣時間

  return date.toISOString().replace("T", " ").split(".")[0]; // 格式化輸出 YYYY-MM-DD HH:mm:ss

}



沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。

【Google試算表教學】一步驟解決!徹底防止檢視者下載、複製,保護你隱藏的工作表

您是否也以為只要將Google試算表中的工作表「隱藏」起來,分享出去後別人就看不到了呢?最近有網友反應,他明明已經將含有原始資料的工作表隱藏了,但分享給同事後,對方卻說依然能看到隱藏的內容。 經過測試後,我們發現了一個許多人都可能忽略的資安漏洞: 即使你隱藏了工作表,只要檢視者有...