简体   繁体   English

适用于雅虎财经的 Google Apps 脚本返回空单元格

[英]Google Apps Script for Yahoo Finance Returns Empty Cell

A previously working solution that was resolved here by @tanaike suddenly returns an empty cell upon execution. @tanaike 在这里解决的以前有效的解决方案在执行时突然返回一个空单元格。 I don't get an error message and in the google apps scripts edit page I get "Notice Execution completed".我没有收到错误消息,并且在 Google Apps 脚本编辑页面中我收到“通知执行已完成”。

It looks like it's working in the background but having trouble returning a value to the cell, my guess would be something wrong with the last line that may resolve it?看起来它在后台工作但无法将值返回到单元格,我猜最后一行可能会解决问题?

function pressReleases(code) {
  var url = 'https://finance.yahoo.com/quote/' + code + '/press-releases'
  var html = UrlFetchApp.fetch(url).getContentText().match(/root.App.main = ([\s\S\w]+?);\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";
}

The CryptoJS code saved as a script in google apps script is here在谷歌应用程序脚本中保存为脚本的 CryptoJS 代码在这里

When I tested this script, at if (;_cs || !_cr) return;当我测试这个脚本时,在if (;_cs || !_cr) return; , I confirmed that the values of _cs and _cr are undefined . ,我确认_cs_cr的值是undefined From this result, I understood that recently, the specification of the key for decrypting the data has been changed at the server side.从这个结果,我了解到最近在服务器端更改了用于解密数据的密钥规范。 When I saw this thread , I confirmed the same situation.当我看到这个线程时,我确认了同样的情况。 In the thread, I noticed that, in the current stage, the key can be simply retrieved from the HTML data.在线程中,我注意到,在当前阶段,可以简单地从 HTML 数据中检索密钥。 So, as with the current script, how about the following modification?那么,与当前脚本一样,进行以下修改如何?

Usage:用法:

1. Get crypto-js. 1.获取crypto-js。

Please access https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js .请访问https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js And, copy and paste the script to the script editor of Google Apps Script, and save the script.然后,将脚本复制并粘贴到 Google Apps Script 的脚本编辑器中,并保存脚本。

2. Modify 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 = ([\s\S\w]+?);\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";
}
  • When this script is run with code = "PGEN" , the value of Precigen Provides Pipeline and Corporate Updates at the 41st Annual JP Morgan Healthcare Conference is obtained.当使用code = "PGEN"运行此脚本时,将获得Precigen Provides Pipeline and Corporate Updates at the 41st Annual JP Morgan Healthcare Conference的值。

Note:笔记:

  • If you want to load crypto-js directly , you can also use the following script.如果想crypto-js directly ,也可以使用下面的脚本。 But, in this case, the process cost becomes higher than that of the above flow.但是,在这种情况下,处理成本变得高于上述流程。 Please be careful about this.请注意这一点。

     const cdnjs = "https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"; eval(UrlFetchApp.fetch(cdnjs).getContentText());
  • I can confirm that this method can be used for the current situation (January 14, 2023).我可以确认这种方法可以用于当前情况(2023 年 1 月 14 日)。 But, when the specification in the data and HTML is changed in the future update on the server side, this script might not be able to be used.但是,当数据和 HTML 中的规范在服务器端的未来更新中发生变化时,此脚本可能无法使用。 Please be careful about this.请注意这一点。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM