简体   繁体   English

我试图在谷歌表格中使用 importxml 从谷歌金融获取股票价格,在 xml 元素和语法中苦苦挣扎

[英]Im trying to use importxml in google sheets to get a stock price from google finance, struggling with xml elements and syntax

I'm trying to use the IMPORTXML() function in google sheets to get a current stock price from Yahoo finance, anyway, I have a pretty decent understanding of google app script and java but no XML background what-so-ever.我正在尝试使用谷歌表格中的IMPORTXML() function 从雅虎财经获取当前股价,无论如何,我对谷歌应用脚本和 java 有相当不错的理解,但没有 Z3501BB093D363CFED9108B6 背景

From the link I'm trying to get the stock current stock price.链接中,我试图获取股票当前的股价。

Using the Inspector in google chrome I get these values as follows of the element I need aka "stock price" values as follows:使用谷歌浏览器中的检查器,我得到这些值如下我需要的元素,即“股票价格”值,如下所示:

copy xpath returns:复制 xpath 返回:

//*[@id="quote-header-info"]/div[3]/div[1]/div/span[1]; //*[@id="quote-header-info"]/div[3]/div[1]/div/span[1];

copy full xpath returns:复制完整的 xpath 返回:

/html/body/div[1]/div/div/div[1]/div/div[2]/div/div/div[4]/div/div/div/div[3]/div[1]/div/span[1]; /html/body/div[1]/div/div/div[1]/div/div[2]/div/div/div[4]/div/div/div/div[3]/div[1] /div/span[1];

copy element returns:复制元素返回:

<span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="52">298.88</span> ;

I'm wondering if using part or all of any of those XML codes will return the price ( current price is 298.88 as in the copy element section)我想知道是否使用部分或全部 XML 代码将返回价格(当前价格为 298.88,如复制元素部分)

to the best of my knowledge using =IMPORTXML ("Link goes here", XML code goes here") will return what I need.据我所知,使用=IMPORTXML (“链接在此处”,XML 代码在此处“)将返回我需要的内容。

I just don't know if I'm wrong or what part of code to use thanks in advance.我只是不知道我是否错了或使用哪部分代码提前谢谢。

my best guess as to what i need but returns false is:我对我需要但返回 false 的最佳猜测是:

=IMPORTXML("https://finance.yahoo.com/quote/SPY?p=SPY","data-reactid='52'")

The =IMPORTXML() function takes as parameters a URL and an XPath . =IMPORTXML() function 将 URL 和XPath作为参数。 In your formula, the "data-reactid='52' is not a valid XPath.在您的公式中, "data-reactid='52'不是有效的 XPath。

The following Sheets formula attained the desired result for me:以下表格公式为我达到了预期的结果:

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

As you can see here:正如你在这里看到的:

公式结果

Disclaimer免责声明

You have to consider that the XPath you get in Google Chrome's devtools is obtained based on the current page status (ie after the Javascript code has been parsed and executed).你必须考虑到你在谷歌浏览器的devtools中得到的XPath是基于当前页面状态得到的(即Javascript代码被解析执行后)。 When using =IMPORTXML() function, the engine does not execute the code, thus the XPath you get from the devtools may not work as expected in your Sheets document.使用=IMPORTXML() function 时,引擎不会执行代码,因此您从 devtools 获得的 XPath 可能无法在您的表格文档中按预期工作。

Try below, mind the punctuation mark for 'quote-header-info'.试试下面,注意'quote-header-info'的标点符号。

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

https://docs.google.com/spreadsheets/d/1MWe-aTZFxSQ5IiMyTZrh1HJzQ5Zm3vMJyx6T_5jRVaQ/edit#gid=0 https://docs.google.com/spreadsheets/d/1MWe-aTZFxSQ5IiMyTZrh1HJzQ5Zm3vMJyx6T_5jRVaQ/edit#gid=0

set cell A2 = https://finance.yahoo.com/quote/SPY?p=SPY设置单元格 A2 = https://finance.yahoo.com/quote/SPY?p=SPY
set cell A3 = //*[@id="quote-header-info"]/div[3]/div[1]/div/span[1]设置单元格 A3 = //*[@id="quote-header-info"]/div[3]/div[1]/div/span[1]

set cell A5 = IMPORTXML(A2,A3)设置单元格 A5 = IMPORTXML(A2,A3)

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

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