【发布时间】:2011-07-18 20:54:09
【问题描述】:
我想访问我在 R 中的 google 金融投资组合中的数据。我正在尝试通过阅读 Google Finance API 文档并遵循 RGoogleDocs 的指导来做到这一点。我取得了一些进展,但在解析谷歌金融投资组合的 XML 版本时遇到了很多麻烦。
require(RGoogleDocs)
#Autheticate using RGoogleDocs
auth = getGoogleAuth("me@gmail.com", "password",service="finance")
con = getGoogleDocsConnection(auth)
#Get positions
positions <- getURL("http://finance.google.com/finance/feeds/default/portfolios/6/positions",curl=con)
positions <- xmlInternalTreeParse(positions)
positions['entry'] #Returns nothing, should return a list of stocks
xpathApply(positions, "//a") #Also returns nothing
这是一个例子。我正在尝试解析此文档中的所有“条目”元素。
require(XML)
positions <- "<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gf='http://schemas.google.com/finance/2007' xmlns:gd='http://schemas.google.com/g/2005'><id>http://finance.google.com/finance/feeds/me@gmail.com/portfolios/7/positions/NYSE:SPY</id><updated>2011-07-18T21:42:07.000Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/finance/2007#position'/><title type='text'>SPDR S&P 500 ETF</title><link rel='self' type='application/atom+xml' href='http://finance.google.com/finance/feeds/default/portfolios/7/positions/NYSE%3ASPY'/><gd:feedLink href='http://finance.google.com/finance/feeds/me@gmail.com/portfolios/7/positions/NYSE:SPY/transactions'/><gf:positionData gainPercentage='0.0' return1w='0.0' return1y='0.0' return3m='0.0' return3y='0.0' return4w='0.0' return5y='0.0' returnOverall='0.0' returnYTD='0.0' shares='0.0'/><gf:symbol exchange='NYSE' fullName='SPDR S&P 500 ETF' symbol='SPY'/></entry>"
positions <- xmlInternalTreeParse(positions)
positions['entry']
编辑:
由于某种原因,我发布的示例 XML 字符串与与 google 文档的实时连接不太一样。您能够重现此代码的唯一方法是在 google 金融上设置投资组合。请务必记下您的投资组合的 ID。在我的示例中,我使用投资组合 ID 6 (finance/feeds/default/portfolios/6/positions),但您的可能不同。
#Autheticate using RGoogleDocs
require(RGoogleDocs)
auth = getGoogleAuth("me@gmail.com", "password",service="finance")
con = getGoogleDocsConnection(auth)
#Get positions
positions <- getURL("http://finance.google.com/finance/feeds/default/portfolios/6/positions",curl=con)
doc = xmlTreeParse(positions, useInternalNodes = TRUE)
kids = xmlChildren(doc, addFinalizer = NA)
entries <- sapply(kids, "[", "entry")
entries[1]
【问题讨论】:
-
请提供一个可重现的例子。只有您有权访问您的投资组合,而且大多数人可能没有可用于测试您的代码的 Google 财经投资组合。
-
@Joshua Ulrich:我更新了问题,现在看起来怎么样?
-
这还能用吗? [这里][1] 我看到谷歌停止提供数据。 [1]:stackoverflow.com/questions/11544287/oauth-with-yahoo-finance
标签: xml r google-finance google-finance-api