【问题标题】:Pulling Google Finance portfolios into R将 Google Finance 投资组合导入 R
【发布时间】: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&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 文档的实时连接不太一样。您能够重现此代码的唯一方法是在 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


【解决方案1】:

我制作了一个测试组合。经过大部分错误的努力,最后查看了下面 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'

【讨论】:

  • 如果我从上面发布的位置字符串开始,doc = xmlTreeParse(positions, useInternalNodes = TRUE) 有效,kids = xmlChildren(doc, addFinalizer = NA) 有效,但条目是一个空列表。
  • 你对kids$feed有什么好处吗?条目包含在 节点
  • 嗯,我一定搞砸了我发布的示例 XML。当我直接从谷歌金融获取数据时,你的代码运行良好
  • 是的。我确实注意到其中涉及到指针,所以我认为代码需要以正确传递它们的方式进行。我将添加 getURL 行(我从您的设置中复制。)
  • 太好了,效果很好。现在我只需要找出一种将条目提要转换为数据框的好方法。
猜你喜欢
  • 2016-11-01
  • 2022-11-18
  • 2016-07-07
  • 1970-01-01
  • 2011-11-16
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多