简体   繁体   English

Google 表格:来自雅虎财经的 IMPORTXML

[英]Google Sheet: IMPORTXML from Yahoo Finance

I'm trying to import current stock price from yahoo finance.我正在尝试从雅虎财经导入当前股价。 I used a formula from some website and it partially work.我使用了某个网站的公式,它部分起作用。 I only know how to tell it to look for a specific query and it worked fine for some other data point I need but the price change query changes from我只知道如何告诉它寻找一个特定的查询,它对我需要的其他一些数据点工作正常,但价格变化查询从

"Fw(500) Pstart(10px) Fz(24px) C($dataRed)" 

to

"Fw(500) Pstart(10px) Fz(24px) C($dataGreen)" 

depending if the price is up or down for the day.取决于当天价格是上涨还是下跌。

How do I modify the formula I'm using below to use the "or" operator in this case?在这种情况下,如何修改我在下面使用的公式以使用“或”运算符? so that it will pull the price down whether the stock is up or down for the day.这样无论股票当天上涨还是下跌,它都会拉低价格。 Thanks!谢谢!

Formula I'm using: =IMPORTXML("https://finance.yahoo.com/quote/IBM","//span[@class='Fw(500) Pstart(10px) Fz(24px) C($dataRed)']")我使用的公式: =IMPORTXML("https://finance.yahoo.com/quote/IBM","//span[@class='Fw(500) Pstart(10px) Fz(24px) C($dataRed)']")

I noticed the other answers did not work for me (they may have worked in the past), so I decided to post this solution.我注意到其他答案对我不起作用(它们过去可能有用),所以我决定发布这个解决方案。 Just put the ticker in cell A1 and one or both of the below formulas somewhere else.只需将股票代码放在单元格A1 中,并将下面的一个或两个公式放在其他地方。

Price:价钱:

=IFNA(VALUE(IMPORTXML("https://finance.yahoo.com/quote/" & A1, "//*[@class=""D(ib) Mend(20px)""]/span[1]")))

Change:改变:

=IFNA(VALUE(REGEXEXTRACT(IMPORTXML("https://finance.yahoo.com/quote/" & A1,"//*[@class=""D(ib) Mend(20px)""]/span[2]"), "^.*?\s")))

You could use a more dynamic/generic xpath that doesnt require such specific paths such as this:您可以使用不需要此类特定路径的更动态/通用的 xpath,例如:

This one pulls in both the price and the change:这个同时引入了价格和变化:

=ARRAY_CONSTRAIN(transpose(IMPORTXML("https://finance.yahoo.com/quote/IBM:,"//*[@class='Mt(6px)']//span")),1,2)

If you just want the price:如果你只想要价格:

=trim(IMPORTXML("https://finance.yahoo.com/quote/IBM","//*[@class='Mt(6px)']//span"))

If you just want the change:如果你只是想要改变:

=IMPORTXML("https://finance.yahoo.com/quote/IBM","//*[@class='Mt(6px)']//span[2]")

在此处输入图片说明

If you just want the price:如果你只想要价格:

=IFNA(VALUE(IMPORTXML("https://finance.yahoo.com/quote/" & $A1, "//*[@class=""D(ib) Mend(20px)""]/span[1]"))) =IFNA(VALUE(IMPORTXML("https://finance.yahoo.com/quote/" & $A1, "//*[@class=""D(ib) Mend(20px)""]/span[1) ]")))

currently using googlefinance api but find it does not update often enough even when updates set to every minute so currently testing if below will allow updates at least with an F5 press within the sheet目前正在使用 googlefinance api,但发现即使将更新设置为每分钟更新一次,它也不会经常更新,因此目前正在测试下面是否允许至少在工作表中按 F5 键进行更新

this brings in the price and other information (dated 2022/09/27)这带来了价格和其他信息(日期为 2022/09/27)

=IMPORTXML("https://finance.yahoo.com/quote/SAVA/", "//*[@id=""quote-header-info""]/div[3]/div[1]/div[1]") =IMPORTXML("https://finance.yahoo.com/quote/SAVA/", "//*[@id=""quote-header-info""]/div[3]/div[1]/div [1]")

Sadly Yahoo Finance changes the XML/HTML structure of its website quite often.遗憾的是,雅虎财经经常更改其网站的 XML/HTML 结构。 The one that works for now is:现在有效的是:

=IMPORTXML("https://finance.yahoo.com/quote/IBM/", "//*[@id=""quote-header-info""]/div[3]/div[1]/div/span[1]")

You may always open the HTML structure and use the developer tools to find and copy the X-path.您可以随时打开 HTML 结构并使用开发人员工具来查找和复制 X 路径。

PS1. PS1。 Though there seem to be a bug and the function can't retrieve data from URLs where there is a dot/point/period " . " in the name.尽管似乎存在错误,并且该函数无法从名称中带有点/点/句点“ . ”的 URL 检索数据。

PS2. PS2。 The IMPORTHTML() function can't also fetch the latest price from Yahoo Finance because the information is neither in a table nor a list. IMPORTHTML()函数也不能从雅虎财经获取最新价格,因为信息既不在表格中也不在列表中。 You can try the scripts from this page and this page to list all the table s and list s.您可以尝试此页面此页面中的脚本以列出所有tablelist

或者您可以在单元格上使用条件格式,以便颜色仅由单元格的值确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM