
一、證交所 API
1. 新版 API 這是證交所最新版本 API 網址:
https://openapi.twse.com.tw/v1/exchangeReport/MI_INDEX

[
{"指數":"發行量加權股價指數","收盤指數":"12946","漲跌":"-","漲跌點數":"30.66","漲跌百分比":"-0.24","特殊處理註記":""},
{"指數":"臺灣50指數","收盤指數":"9739","漲跌":"+","漲跌點數":"8.99","漲跌百分比":"0.09","特殊處理註記":""},
{"指數":"臺灣中型100指數","收盤指數":"11551","漲跌":"-","漲跌點數":"56.17","漲跌百分比":"-0.48","特殊處理註記":""},
{"指數":"未含金融指數","收盤指數":"11089","漲跌":"-","漲跌點數":"28.06","漲跌百分比":"-0.25","特殊處理註記":""},
{"指數":"未含電子指數","收盤指數":"15597","漲跌":"-","漲跌點數":"97.67","漲跌百分比":"-0.62","特殊處理註記":""},
]
2. 舊版 API
以前舊的 API 有兩種呼叫網址:
- https://mis.twse.com.tw/stock/api/
- https://www.twse.com.tw/exchangeReport/
- 沒有官方說明書
- 有可能某個參數忽然不能用了
- 有可能無預警失效
- 新版 API 呼叫網址為 https://openapi.twse.com.tw/v1/exchangeReport/FMSRFK_ALL → 會撈回所有股票
- 舊版 API 可這麼使用 https://www.twse.com.tw/exchangeReport/FMSRFK?date=20220901&stockNo=2330
- 可加入日期參數、股票代號
- 此網址可撈到台積電今年(2022)1~9月的月成交資訊
二、資料庫與寄信
使用 API 抓回資料後,需要儲存在資料庫,本篇會以「Google 試算表作為資料庫」,需使用 Google Apps Script(以下簡稱GAS)讀寫資料庫,可參考「用 Google Apps Script 操作 Google 試算表」系列文章。 使用排程抓資料的技巧、流程,可參考之前寫的「使用 Node.js 爬蟲定期抓網頁資料,結合 Google 試算表作為資料庫」,如果習慣使用 NodeJs 可參考該篇。 本篇提供的範例程式碼以 GAS 環境為主,資料存入 Google試算表後,再整理成需要的資訊格式。利用 GAS 操作 Gmail API,寄出郵件給自己,就完成了整個流程。三、抓大盤資料範例
1. Google 試算表

var ss = SpreadsheetApp.getActiveSpreadsheet(),
index_sheet = ss.getSheetByName("大盤"), // 填入自己設定的工作表名稱
email = "xxxxx@gmail.com"; // 填入要收到通知的 email
// 取得大盤成交資訊
function getIndexData() {
var apiUrl = "https://www.twse.com.tw/exchangeReport/FMTQIK", // api 呼叫網址
dailyRow = [],
today = new Date(),
todayFormate = Utilities.formatDate(today, "GMT+8", "yyyyMMdd"), // 格式化今天日期
twYear = today.getFullYear() - 1911, // 中華民國年份
todayTwFormat = Utilities.formatDate(today, "GMT+8", twYear + "/MM/dd"), // 中華民國日期格式
todayEmailFormat = Utilities.formatDate(today, "GMT+8", "yyyy-MM-dd"), // 郵件日期格式化
fetchUrl, response, json, i, data, closeIndex, variation, volume;
// 組合 api 參數, 加上今天日期
fetchUrl = apiUrl + "?date=" + todayFormate;
// 呼叫 api 取得大盤成交資訊
response = UrlFetchApp.fetch(fetchUrl).getContentText();
json = JSON.parse(response);
data = json.data;
for (i in data) {
// 只取今日資料
if (data[i][0] == todayTwFormat) {
closeIndex = removeComma(data[i][4]); // 收盤指數 數字格式化
variation = removeComma(data[i][5]); // 漲跌點數 數字格式化
volume = removeComma(data[i][2]); // 成交量 數字格式化
// 整理成交量格式為(億)
volume = parseInt(volume / 100000000);
dailyRow = [todayEmailFormat, closeIndex, variation, volume];
}
}
// 如果有今日資料
if (dailyRow.length) {
// 工作表增加一列資料
index_sheet.appendRow(dailyRow);
// 寄信通知
sendMailIndex(dailyRow);
}
// 數字格式化
function removeComma(number) {
return number.replace(/,/g, "");
}
}
// 寄信通知
function sendMailIndex(dailyRow) {
var subject = "每日大盤成交資訊" + " " + dailyRow[0], // 信件標題
htmlBody = "";
// 組合信件內容
htmlBody += "日期:" + dailyRow[0] + "<br/>";
htmlBody += "收盤指數:" + dailyRow[1] + "<br/>";
htmlBody += "漲跌點數:" + dailyRow[2] + "<br/>";
htmlBody += "成交量(億):" + dailyRow[3] + "<br/>";
MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: htmlBody
});
}
- 可參考註解文字修改參數,填入自己的 email
- 可試著執行主程式 getIndexData()
- 第一次執行 GAS 會要求授權,按畫面指示提供權限即可

四、抓個股資料範例
1. 範例程式碼 以下為 GAS「抓個股資料」(台積電 2330)的範例:var ss = SpreadsheetApp.getActiveSpreadsheet(),
stockNo = "2330", // 填入個股代號
index_sheet = ss.getSheetByName("2330"), // 填入自己設定的工作表名稱
email = "xxxxx@gmail.com"; // 填入要收到通知的 email
// 取得個股成交資訊
function getStockData() {
var apiUrl = "https://www.twse.com.tw/exchangeReport/STOCK_DAY", // api 呼叫網址
dailyRow = [],
today = new Date(),
todayFormate = Utilities.formatDate(today, "GMT+8", "yyyyMMdd"), // 格式化今天日期
twYear = today.getFullYear() - 1911, // 中華民國年份
todayTwFormat = Utilities.formatDate(today, "GMT+8", twYear + "/MM/dd"), // 中華民國日期格式
todayEmailFormat = Utilities.formatDate(today, "GMT+8", "yyyy-MM-dd"), // 郵件日期格式化
fetchUrl, response, json, i, data, closePrice, variation, volume;
// 組合 api 參數, 加上今天日期、股票代號
fetchUrl = apiUrl + "?date=" + todayFormate + "&stockNo=" + stockNo;
// 呼叫 api 取得大盤成交資訊
response = UrlFetchApp.fetch(fetchUrl).getContentText();
json = JSON.parse(response);
data = json.data;
for (i in data) {
// 只取今日資料
if (data[i][0] == todayTwFormat) {
closePrice = removeComma(data[i][6]); // 收盤指數 數字格式化
variation = removeComma(data[i][7]); // 漲跌點數 數字格式化
volume = removeComma(data[i][2]); // 成交量 數字格式化
// 整理成交量格式為(億)
volume = parseInt(volume / 100000000);
dailyRow = [todayEmailFormat, closePrice, variation, volume];
}
}
// 如果有今日資料
if (dailyRow.length) {
// 工作表增加一列資料
index_sheet.appendRow(dailyRow);
// 寄信通知
sendMailStock(dailyRow);
}
// 數字格式化
function removeComma(number) {
return number.replace(/,/g, "");
}
}
// 寄信通知
function sendMailStock(dailyRow) {
var subject = "每日 " + stockNo + " 成交資訊" + " " + dailyRow[0], // 信件標題
htmlBody = "";
// 組合信件內容
htmlBody += "日期:" + dailyRow[0] + "<br/>";
htmlBody += "收盤價:" + dailyRow[1] + "<br/>";
htmlBody += "漲跌價差:" + dailyRow[2] + "<br/>";
htmlBody += "成交金額(億):" + dailyRow[3] + "<br/>";
MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: htmlBody
});
}
一樣參考註解文字修改參數即可。
2. 查詢資料庫

五、設定排程
最後一個步驟為設定排程,讓 GAS 能每日定時執行程式。
- A:點擊左側「觸發條件」圖示
- B:按右下角「新增觸發條件」按鈕
- C:選擇觸發的函數名稱
- D:選擇「時間驅動」
- E:選擇「日計時器」
- F:選擇觸發時間,選擇 13:30 收盤以後的時間比較恰當
- 最後按「儲存」即可
更多 Google Apps Script 相關文章:
上市有寫怎麼取資料
回覆刪除那上櫃要怎麼取?