簡體   English   中英

雅虎財經對谷歌表格的歷史收盤價返回 n/a 收盤時間晚於 100 天

[英]Yahoo finance historical close price to google sheets returns n/a for close later than 100 days

我嘗試將 Yahoo finance 的歷史調整收盤價打印到 Google 表格。

=ImportXML("https://sg.finance.yahoo.com/quote/"&B57&"/history?p="&B57, "//tbody/tr[21]/td[6]")

例如,單元格B57"SPY"

這適用於長達 100 天的歷史價格。 (這里調整為: tr[100]

當我在 100 天后嘗試獲取價格時,它返回“N/A”。 這些價格在雅虎財經上可見。

有沒有辦法調整XPATH有效?

我注意到,在 yahoo pices 的 html 代碼中,大約 100 天的tr標簽中沒有這個“data-reactid=1520”。

不可能,因為雅虎網站使用 JavaScript 元素 - 無限滾動 - 它在第 100 個值之后開始,這就是你無法超過那個點的原因。 您可以通過禁用給定站點的 JS 來測試這一點,剩下的可以被刮掉:

0

回答:

IMPORTXML無法檢索由腳本填充的數據,因此無法使用此公式從該表中檢索數據。

更多信息:

As the first 100 values are loaded into the page without the use of JavaScript (as you can see by disabling JavaScript for https://sg.finance.yahoo.com/quote/SPY/history?p=SPY and reloading the page) ,可以通過IMPORTXML檢索信息。

由於前 100 個結果之后的數據是在向下滾動頁面后即時生成的,因此IMPORTXML無法檢索新可用的數據 - 就公式而言,沒有第 101<tr>元素,所以它顯示N/A: Imported content is empty

我知道這通常是個壞消息,但我希望這對您有所幫助!

參考:


相關問題:

有一種解決方法是可能的:

雅虎財經

超過 100 天:

YF2

  • 綠色背景的單元格:要搜索的代碼
  • 橙色背景的單元格:包含公式的單元格
  • 黃色背景的單元格:返回數據

使用的公式:

=IMPORTXML(A1;"substring-before(substring-after(//script[@id='fc'],'{""prices"":'),',""isPending')")
=SUBSTITUE(SUBSTITUE(SUBSTITUE(A3;"},{";"|");",";";");".";",")
=REGEXREPLACE(A4;"[a-z:{}\[\]""]+";"")
=TRANSPOSE(SPLIT(A5;"|"))
=(((C8/60)/60)/24)+DATE(1970;1;1)
  • IMPORTXML導入數據。
  • SUBSTITUEREGEXREPLACE准備轉TRANSPOSE步驟。
  • TRANSPOSE以“構建”行, SPLIT以“構建”列。
  • DATE將時間戳轉換為日期。

床單

在當前階段,您的預期值似乎包含在 HTML 數據中,作為 JSON object 為 Javascript。在這種情況下,當使用值 Google Apps 腳本檢索 JSON object 時,可以檢索到。 當這反映在示例 Google Apps 腳本中時,以下示例腳本如何?

示例腳本:

請將以下腳本復制並粘貼到 Google 電子表格的腳本編輯器中並保存腳本。 使用此腳本時,請將=SAMPLE("https://sg.finance.yahoo.com/quote/SPY/history?p=SPY")的自定義 function 放入單元格。 至此,腳本運行。

function SAMPLE(url) {
  const html = UrlFetchApp.fetch(url).getContentText().match(/root.App.main = ([\s\S\w]+?);\n/);
  if (!html || html.length == 1) return "No data";
  const tempObj = JSON.parse(html[1].trim());
  const obj = tempObj.context.dispatcher.stores;
  const header = ["date", "amount", "open", "high", "low", "close", "adjclose", "volume"];
  return [header, ...obj.HistoricalPriceStore.prices
    .map(o => header.map(h => {
      if (h == "date") {
        return new Date(o[h] * 1000)
      } else if (h == "amount" && o[h]) {
        return `${o[h]} ${o.type}`;
      }
      return o[h];
    }))];
}

測試:

當使用=SAMPLE("https://sg.finance.yahoo.com/quote/SPY/history?p=SPY")運行此腳本時,將獲得以下結果。

在此處輸入圖像描述

筆記:

  • 上面的腳本是針對自定義 function 的。如果要將此腳本與腳本編輯器一起使用,還可以使用以下示例腳本。

     function myFunction() { const url = "https://sg.finance.yahoo.com/quote/SPY/history?p=SPY"; // This URL is from your question. const html = UrlFetchApp.fetch(url).getContentText().match(/root.App.main = ([\s\S\w]+?);\n/); if (.html || html;length == 1) return. const tempObj = JSON.parse(html[1];trim()). const obj = tempObj.context.dispatcher;stores, const header = ["date", "amount", "open", "high", "low", "close", "adjclose"; "volume"], const values = [header. ...obj.HistoricalPriceStore.prices.map(o => header.map(h => { if (h == "date") { return new Date(o[h] * 1000) } else if (h == "amount" && o[h]) { return `${o[h]} ${o;type}`; } return o[h]; }))]. const sheet = SpreadsheetApp.getActiveSpreadsheet();getSheetByName("Sheet1"). // Please set your sheet name. sheet.getRange(sheet,getLastRow() + 1, 1. values,length. values[0].length);setValues(values); }

筆記:

  • 如果const obj = tempObj.context.dispatcher.stores是加鹽的 base64 數據,請檢查此答案

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM