8

我想访问我在 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&amp;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&amp;P 500 ETF' symbol='SPY'/></entry>"
positions <- xmlInternalTreeParse(positions)
positions['entry']

编辑:

由于某种原因,我发布的示例 XML 字符串与谷歌文档的实时连接不太一样。您能够重现此代码的唯一方法是在 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]
4

1 回答 1

5

我做了一个测试组合。经过大部分错误的努力,最后查看了下面的 doc 对象的类和可用的方法,我想到了这个从 GoogleFinance 投资组合中提取条目的策略:

auth = getGoogleAuth("your_goog_id@gmail.com", "yourpwd123",service="finance")
con = getGoogleDocsConnection(auth)

#Get positions
positions <- getURL("http://www.google.com/finance/portfolio?action=view&pid=1",curl=con)
   # Parse
doc = xmlTreeParse(positions, useInternalNodes = TRUE)
   # Convert to an R accessible form
kids = xmlChildren(doc, addFinalizer = NA)
   # access with `$` or "[" functions
entries <- sapply(kids, "[", "entry")
entries[1]   #  You may need to adjust this index if you get more than one 'entry'
于 2011-07-19T04:30:09.847 回答