从雅虎财经获取信息

问题描述 投票:0回答:2

我想使用 importxml 从此网站获取市值: https://finance.yahoo.com/quote/KIT.OL?p=KIT.OL&.tsrc=fin-srch

上面写着 3.217B。

我用它来获取“前一个收盘价”值:

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

我希望我可以调整上面的公式来获得市值。有谁可以帮忙吗?

谢谢!

google-sheets web-scraping xpath google-sheets-formula yahoo-finance
2个回答
2
投票

尝试:

=INDEX(IMPORTXML(A1, "//tr"), 9, 2)

或:

=INDEX(QUERY(TO_TEXT(IMPORTXML(A1, "//tr")), 
 "select Col2 where Col1 = 'Market Cap'", 0))



但是!

这样你只能得到旧的值。要获取新的,您需要使用脚本:

function YAHOO(url) {
  const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  const tables = [...res.getContentText().matchAll(/(<table[\w\s\S]+?<\/table>)/g)];
  if (tables.length < 2) return "No tables. Please confirm URL again.";
  const values = tables.reduce((ar, [,table]) => {
    if (table) {
      const root = XmlService.parse(table).getRootElement();
      const temp = root.getChild("tbody", root.getNamespace()).getChildren().map(e => e.getChildren().map(f => isNaN(f.getValue()) ? f.getValue() : Number(f.getValue())));
      ar = ar.concat(temp);
    }
    return ar;
  }, []);
  return values[0].map((_, i) => values.map(r => r[i]));
}

和公式:

=INDEX(YAHOO(A1), 2, 9)

额外阅读:https://stackoverflow.com/a/65914858/5632629


0
投票

您可以尝试使用完整的 XPath:

=IMPORTXML(A1,"/html/body/div[1]/div/div/div[1]/div/div[3]/div[1]/div/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[1]/td[2]/span")

或者你可以尝试一下

vlookup()
:

=vlookup("Market Cap", IMPORTXML(A1,"//tr"),2,0)
© www.soinside.com 2019 - 2024. All rights reserved.