【问题标题】:Google Sheet: IMPORTXML from Yahoo Finance fails for some stocks谷歌表格:雅虎财经的 IMPORTXML 对某些股票失败
【发布时间】:2021-04-05 21:42:21
【问题描述】:

我正在尝试通过谷歌金融不提供的 IMPORTXML 将雅虎金融中的一些报价导入谷歌表格。 例如,从 IBM 或 Apple 导入当前汇率,我使用 =IFNA(VALUE(IMPORTXML("https://finance.yahoo.com/quote/IBM","//*[@class=""D(ib) Mend(20px)""]/span[1]")))

=IFNA(VALUE(IMPORTXML("https://finance.yahoo.com/quote/AAPL","//*[@class=""D(ib) Mend(20px)""]/span[1]")))

到目前为止,这有效。 但是,我没有输入这家公司的价格。 https://finance.yahoo.com/quote/0P00016MCQ.F

我只是不知道为什么以及该怎么做。有人能帮我吗?我会很高兴的!

【问题讨论】:

    标签: google-sheets-formula yahoo-finance


    【解决方案1】:

    我无法回答为什么您无法为“全球在线零售”获取数据而您可以为“IBM”获取数据的原因。不过,正确的做法是查看页面的源代码,尤其是名为“root.App.main”的 json,其中包含您需要的所有信息。

    function marketPrice() {
      var code = 'C2PU.SI'
      var info = 'regularMarketPrice'
    
      var url='https://finance.yahoo.com/quote/' + code
      var source = UrlFetchApp.fetch(url).getContentText()
      var jsonString = source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
      var data = JSON.parse(jsonString)
      var price = data.context.dispatcher.stores.StreamDataStore.quoteData.item(code).item(info).raw
      Logger.log(price)
    }
    Object.prototype.item=function(i){return this[i]};
    

    【讨论】:

    • 嗨迈克!非常感谢您的提示。我会仔细看看,也许我可以用它解决问题。目前我仍然收到一条错误消息(异常:一天内调用了太多次服务:urlfetch.marketPrice@Code.gs:3)。但我认为这只是暂时的。
    • 我已经更新了代码和实现的参数,使用起来更加灵活。
    猜你喜欢
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2013-11-06
    • 2014-11-03
    • 1970-01-01
    相关资源
    最近更新 更多