8

R で Google Finance ポートフォリオのデータにアクセスしたいと考えています。Google Finance APIドキュメントを読み、 RGoogleDocsの指示に従ってこれを実行しようとしています。私はある程度の進歩を遂げましたが、Google Finance ポートフォリオの 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 Finance でポートフォリオを設定することです。ポートフォリオの 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 に答える