首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将谷歌金融投资组合纳入R

将谷歌金融投资组合纳入R
EN

Stack Overflow用户
提问于 2011-07-18 20:54:09
回答 1查看 2.3K关注 0票数 8

我想在R中访问我的中的数据,我试图通过阅读Google Finance API文档并跟随RGoogleDocs来实现这一点。我已经取得了一些进展,但是我在解析的XML版本时遇到了很多困难。

代码语言:javascript
复制
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

下面是一个例子。我正试图解析本文档中的所有“entry”元素。

代码语言:javascript
复制
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。在我的示例中,我使用PortfolioID6 (finance/feeds/default/portfolios/6/positions),但您的可能有所不同。

代码语言:javascript
复制
#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]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-19 04:30:09

我做了个测试组合。在进行了大部分错误的工作之后,并最终查看了下面的doc对象类和可用的方法,我想到了从GoogleFinance组合中提取条目的策略:

代码语言:javascript
复制
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'
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6739309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档