【问题标题】:How can I use JSON to fetch a single piece of data from a webpage如何使用 JSON 从网页中获取单个数据
【发布时间】:2021-02-13 00:55:47
【问题描述】:

我正在使用 JSON 从 API(FMP 云)中为我的 Google 表格电子表格检索股市数据,但不知道如何从 API 网页获取特定数据。以下是我试图从中提取的网页内容:

[ {
  "symbol" : "AMC",
  "name" : "AMC Entertainment Holdings, Inc.",
  "price" : 5.60000000,
  "changesPercentage" : -0.18000000,
  "change" : -0.01000000,
  "dayLow" : 5.55000000,
  "dayHigh" : 5.97000000,
  "yearHigh" : 20.36000000,
  "yearLow" : 1.91000000,
  "marketCap" : 1898808704.00000000,
  "priceAvg50" : 4.95848460,
  "priceAvg200" : 4.37841750,
  "volume" : 39477325,
  "avgVolume" : 120395740,
  "exchange" : "NYSE",
  "open" : 5.72000000,
  "previousClose" : 5.61000000,
  "eps" : -34.81800000,
  "pe" : null,
  "earningsAnnouncement" : "2020-11-02T16:15:00.000+0000",
  "sharesOutstanding" : 339072983,
  "timestamp" : 1613160110
} ]

如果我使用单元格脚本=IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "/price","")(我替换了我的真实 API 密钥),它会获取其中包含“价格”一词的所有数据,即价格、priceAvg50 和 priceAvg200。它看起来像这样:

    Avg50   Avg200
5.59    4.9584846   4.3784175

如果我只想要价格,即第一条数据(5.59),我应该如何修改单元格中的脚本?希望这个问题是有道理的。感谢您的宝贵时间。

【问题讨论】:

  • 虽然我不确定你使用的是IMPORTJSON,但如果数据的结构始终相同,那么使用const IMPORTJSON = url => JSON.parse(UrlFetchApp.fetch(url).getContentText())[0].price;的脚本怎么样?在这种情况下,请将=IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey") 的公式放入单元格中。
  • 您好,感谢您的帮助。当我尝试这个时,它会将我可以获取的东西限制在价格上。所有带有脚本以获取其他内容的单元格都会出现错误。有没有办法绕过它?
  • 感谢您的回复。我带来的不便表示歉意。在我的评论中,来自If I just want price, which is the first piece of data (5.59), how should I modify the script in the cell?,我提出了它。关于When I try this, it restricts what I can fetch to just the price. All cells with script looking to fetch other things come up as an error. Is there a way to bypass that?在您的回复中,您能否提供您期望的示例输出?通过这个,我想考虑解决方案。对此我深表歉意。
  • 没问题,感谢您抽出宝贵时间帮助我。样本输出将是两个单元格。在单元格 1 中,我会写 =IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "/price", "noHeaders"),它会向我显示股票的价格。在单元格 2 中,我会写 =IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "/changesPercentage", "noHeaders"),这将显示股票价格的百分比变化。
  • 感谢您的回复。从您的 2 个回复 cmets 中,我提出了一个答案。你能确认一下吗?如果这不是您期望的方向,我深表歉意。

标签: json google-apps-script google-sheets google-sheets-formula google-sheets-api


【解决方案1】:

从您的以下 2 条回复中,

当我尝试这个时,它会将我可以获取的内容限制在价格范围内。所有带有脚本以获取其他内容的单元格都会出现错误。有没有办法绕过它?

样本输出将是两个单元格。在单元格 1 中,我会写 =IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "/price", "noHeaders"),理想情况下会显示股票的价格。在单元格 2 中,我会写 =IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "/changesPercentage", "noHeaders"),这将显示股票价格的百分比变化。

我理解你的目标如下。

  • 您希望通过提供属性名称从问题中的 JSON 数据中检索值。

虽然很遗憾,我无法理解您当前的IMPORTJSON 脚本,但如果您的URL 中的数据结构始终相同,那么直接为您要使用的URL 创建脚本如何?

示例脚本:

请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中。

function SAMPLE(url, keys) {
  const obj = JSON.parse(UrlFetchApp.fetch(url).getContentText());
  return [keys.split(",").map(k => obj[0][k.trim()] || "")];
}
  • 使用此脚本时,请输入=SAMPLE("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "price")=SAMPLE("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "changesPercentage")
  • 如果您想通过一个自定义论坛检索多个值,请输入=SAMPLE("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "price,changesPercentage")
    • 使用上述示例数据时,此公式将5.6, -0.18 返回到一行。
  • 如果给出的属性名称在来自 URL 的数据中不存在,则返回空值。

参考资料:

【讨论】:

    【解决方案2】:
    function hexhead() {
      const d=JSON.parse('[ {  "symbol" : "AMC",  "name" : "AMC Entertainment Holdings, Inc.",  "price" : 5.60000000,  "changesPercentage" : -0.18000000,  "change" : -0.01000000,  "dayLow" : 5.55000000,  "dayHigh" : 5.97000000,  "yearHigh" : 20.36000000,  "yearLow" : 1.91000000,  "marketCap" : 1898808704.00000000,  "priceAvg50" : 4.95848460,  "priceAvg200" : 4.37841750,  "volume" : 39477325,  "avgVolume" : 120395740,  "exchange" : "NYSE",  "open" : 5.72000000,  "previousClose" : 5.61000000,  "eps" : -34.81800000,  "pe" : null,  "earningsAnnouncement" : "2020-11-02T16:15:00.000+0000",  "sharesOutstanding" : 339072983,  "timestamp" : 1613160110} ]');
      console.log(d[0].timestamp)
    }
    
    7:12:44 PM  Notice  Execution started
    7:12:44 PM  Info    1613160110
    7:12:45 PM  Notice  Execution completed
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 2013-04-27
      • 2012-04-21
      相关资源
      最近更新 更多