![](/img/trans.png)
[英]GOOGLE Finance Sheets returns same stock price on different days
[英]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”。
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 天:
使用的公式:
=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
導入數據。SUBSTITUE
和REGEXREPLACE
准備轉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.