This Google Apps Script code to scrape historical data from from Yahoo Finance stopped working yesterday. It suddenly gives the error - No data (data.length == 0).
I think the bug is in the line 8 script while getting the JSON but I dont't have the necessary skill to fix it.
It woud be appreciate your help with issue.
function Scrapeyahoo(symbol) {
//Leemos de yahoo finance historical data
const s = encodeURI(symbol); // so that it works with a url
// turn it into an URL and call it
const url = 'https://finance.yahoo.com/quote/' +s +'/history?p=' +s;
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
const $ = Cheerio.load(res);
const data = $('script').toArray().reduce((ar, x) => {
const c = $(x).get()[0].children;
if (c.length > 0) {
const d = c[0].data.trim().match(/({"context"[\s\S\w]+);\n}\(this\)\);/);
if (d && d.length == 2) {
ar.push(JSON.parse(d[1]));
}
}
return ar;
}, []);
if (data.length == 0) throw new Error("No data.");
const header = ["date", "open", "high", "low", "close", "adjclose", "volume"];
var key = Object.entries(data[0]).find(([k]) => !["context", "plugins"].includes(k))[1];
if (!key) return;
const cdnjs = "https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js";
eval(UrlFetchApp.fetch(cdnjs).getContentText());
const obj1 = data[0];
const obj2 = JSON.parse(CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(obj1.context.dispatcher.stores, key)));
const ar = obj2.HistoricalPriceStore.prices.map(o => header.map(h => h == "date" ? new Date(o[h] * 1000) : (o[h] || "")));
// ---
return ar
}
The original code was modified in December according to this solution, after it stopped working, but I can't find a solution for the issue now.