Google Apps Script for Yahoo Finance返回空单元格



先前由@tanaike解决的解决方案在执行时突然返回一个空单元格。我没有得到一个错误信息,在谷歌应用程序脚本编辑页面,我得到"通知执行完成"

它看起来像它在后台工作,但有麻烦返回一个值到单元格,我的猜测会有问题的最后一行,可以解决它?

function pressReleases(code) {
var url = 'https://finance.yahoo.com/quote/' + code + '/press-releases'
var html = UrlFetchApp.fetch(url).getContentText().match(/root.App.main = ([sSw]+?);n/);
if (!html || html.length == 1) return;
var obj = JSON.parse(html[1].trim());
// --- I modified the below script.
const { _cs, _cr } = obj;
if (!_cs || !_cr) return;
const key = CryptoJS.algo.PBKDF2.create({ keySize: 8 }).compute(_cs, JSON.parse(_cr)).toString();
const obj2 = JSON.parse(CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(obj.context.dispatcher.stores, key)));
var res = obj2.StreamStore.streams["YFINANCE:" + code + ".mega"].data.stream_items[0].title;
// ---
return res || "No value";
}

在google apps脚本中以脚本形式保存的CryptoJS代码在这里

当我测试这个脚本时,在if (!_cs || !_cr) return;,我确认_cs_cr的值是undefined。从这个结果中,我了解到最近在服务器端更改了用于解密数据的密钥规范。当我看到这个帖子时,我确认了同样的情况。在线程中,我注意到,在当前阶段,键可以简单地从HTML数据中检索。那么,与当前脚本一样,下面的修改如何?

用法:

1。得到crypto-js。

请访问https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js。然后,将脚本复制粘贴到Google Apps script的脚本编辑器中,保存脚本。

2。修改脚本。

function pressReleases(code) {
var url = 'https://finance.yahoo.com/quote/' + code + '/press-releases'
var html = UrlFetchApp.fetch(url).getContentText().match(/root.App.main = ([sSw]+?);n/);
if (!html || html.length == 1) return;
var obj = JSON.parse(html[1].trim());
var key = Object.entries(obj).find(([k]) => !["context", "plugins"].includes(k))[1];
if (!key) return;
const obj2 = JSON.parse(CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(obj.context.dispatcher.stores, key)));
var res = obj2.StreamStore.streams["YFINANCE:" + code + ".mega"].data.stream_items[0].title;
// console.log(res); // Check the value in the log.
return res || "No value";
}
  • 当脚本与code = "PGEN"一起运行时,获取Precigen Provides Pipeline and Corporate Updates at the 41st Annual J.P. Morgan Healthcare Conference的值

注意:

  • 如果要加载crypto-js directly,还可以使用以下脚本。但是,在这种情况下,流程成本会高于上述流程的成本。请小心点。

    const cdnjs = "https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js";
    eval(UrlFetchApp.fetch(cdnjs).getContentText());
    
  • 我可以确认这个方法可以用于目前的情况(2023年1月14日)。但是,当数据和HTML中的规范在服务器端的未来更新中发生变化时,该脚本可能无法使用。

  • 参考:

  • crypto-js

最新更新