我想访问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]
答案 0 :(得分: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'