โพสนี้จะมาสอนวิธีการดึงข้อมูลหุ้นจากเว็บไซท์ Yahoo Finance มาบน Google Sheets ผ่านทาง Google App Script กันครับ
วิธีการคือให้เราทำการสร้าง file Google Sheets ขึ้นมาก่อน แล้วทำการเลือกที่เมนู “ส่วนขยาย” และเลือก “Apps Script” เพื่อทำการเข้าไปสร้าง function พิเศษในการดึงข้อมูล

หลังจากนั้นก็ให้เราทำการเอา code ดังต่อไปนี้ไปวางได้เลยครับ
function yahooPrice(ticker) {
const url = `https://finance.yahoo.com/quote/${ticker}?p=${ticker}`;
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const contentText = res.getContentText();
const price = contentText.match(/<fin-streamer(?:.*?)active="">(\d+[,]?[\d\.]+?)<\/fin-streamer>/); return price[1];
}
function yahooPERatio(ticker) {
const url = `https://finance.yahoo.com/quote/${ticker}?p=${ticker}`;
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const contentText = res.getContentText();
const peRatio = contentText.match(/data-test="PE_RATIO-value">(.*?)<\/td>/); return peRatio[1];
}
function yahooEPS(ticker) {
const url = `https://finance.yahoo.com/quote/${ticker}?p=${ticker}`;
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const contentText = res.getContentText();
const eps = contentText.match(/data-test="EPS_RATIO-value">(.*?)<\/td>/); return eps[1];
}
function yahooDividendYield(ticker) {
const url = `https://finance.yahoo.com/quote/${ticker}?p=${ticker}`;
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const contentText = res.getContentText();
const divY= contentText.match(/data-test="DIVIDEND_AND_YIELD-value">(.*?)<\/td>/); return divY[1];
}
function yahooAnalystTarget(ticker) {
const url = `https://finance.yahoo.com/quote/${ticker}?p=${ticker}`;
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const contentText = res.getContentText();
const target= contentText.match(/data-test="ONE_YEAR_TARGET_PRICE-value">(.*?)<\/td>/); return target[1];
}
function yahooBeta(ticker) {
const url = `https://finance.yahoo.com/quote/${ticker}?p=${ticker}`;
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const contentText = res.getContentText();
const beta= contentText.match(/data-test="BETA_5Y-value">(.*?)<\/td>/); return beta[1];
}
โดย code ดังกล่าวจะทำการสร้าง function ใหม่ให้กับ google sheets ของเรา โดยแต่ละ function จะใช้สำหรับการดึงข้อมูลที่แตกต่างกัน เช่น yahooPrice(ชื่อหุ้น) จะใช้ในการดึงข้อมูลราคาของหุ้นมาจากเว็บไซท์ yahoo finance และ yahooEPS(ชื่อหุ้น) ก็จะใช้สำหรับการดึงค่า EPS ของหุ้นนั้นๆมาใน google sheets ของเรานั้นเองครับ

หลังจากนั้นให้ทำการตั้งชื่อ project ที่ด้านบน (ตั้งอย่างไรก็ได้) และทำการกดปุ่ม save
กลับมาที่ file Google Sheets ของเรา เราสามารถพิมพ์ดังภาพด้านล่างเพื่อทำการดึงข้อมูลของหุ้นได้ทันทีครับ

วิธีการวันนี้ผมนำเอามาจากบทความ Medium ของคุณ Jason Hee โดยสามารถกดเข้าไปดูได้ ที่นี้ เลยครับ
และถ้าใครมีคำถามตรงใหน ก็สามารถ comment ทิ้งคำถามไว้ได้เลยนะครับ 🙂
Update 5 Jun 2024
เนื่องจากทาง website ของ google finance มีการเปลี่ยนแปลงด้านการแสดงผลข้อมูลเลยทำให้ script เก่าใช้ไม่ได้นะครับ
แต่ว่าผมได้ทำการปรับแก้ในส่วนของราคาหุ้นให้แล้วครับ ให้เปลี่ยนมาใช้ script ใหม่ตาม code ด้านล่างได้เลยครับ ส่วน code สำหรับดึงข้อมูลในส่วนอื่นๆ เดี๋ยวผมอัพเดทให้ภายหลังอีกครั้งครับ
function yahooPrice(ticker) {
const url = `https://finance.yahoo.com/quote/${ticker}?p=${ticker}`;
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const contentText = res.getContentText();
const price = contentText.match(/data-field="regularMarketPrice"[^>]*?data-value="(\d+[,]?[\d\.]+?)"/); return parseFloat(price[1]);
}
🎥 อุปกรณ์ที่ผมใช้
กล้อง Sony ZV-E10 kit 16-50mm : (https://shope.ee/5KeppTetsX)
Mouse Logitech MX Master 3s: (https://shope.ee/8UbraALvKD)
Mouse Logitech G502 Hero: (https://shope.ee/99rYOea8Z9)
MacBook Air M2: (https://shope.ee/qCQTd8LuD)
ไมค์ wireless Saramonic Blink 500 : (https://shope.ee/2feGcpazuj)
กระเป๋าใส่คอม TATHATA Brand : (https://shope.ee/5fLRlgmRXa)
Copy ไปลงใน app script พอใส่ =yahooPrice(ticker) มันขึ้น error ค่ะ ต้องแก้ตรงไหนคะ รบกวนด้วยค่า
ใช่ครับ ตอนนี้เนื่องจากหน้าเว็บของ yahoo finance มีการแก้ไข เดี๋ยวผมทำการอัพเดท script ให้นะครับ