从雅虎财经获取信息

问题描述

我想使用 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]")

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

谢谢!

解决方法

试试:

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

enter image description here

或:

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

enter image description here



但是!

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

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)

enter image description here

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

,

您可以尝试使用完整的 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,0)