我想要获取通过Yahoo Finance提供的所有共同基金列表,并将其导入R中。在TTR包中有一个stockSymbols函数,但似乎无法获取共同基金。
谢谢。
谢谢。
# Read the list of funds
# I assume the file was downloaded manually from
# http://www.eoddata.com/Data/symbollist.aspx?e=USMF
# This requires registration (free).
d <- read.delim( "USMF.txt", stringsAsFactors = FALSE )
# Retrieve the profile page, for each of the funds.
# It takes 1 second for each, and there are 24,000 of them:
# this may take more than 6 hours.
library(RCurl)
library(stringr)
d$Category <- ""
for( i in seq_len(nrow(d)) ) {
try({
url <- paste0("http://uk.finance.yahoo.com/q/pr?s=", d$Symbol[i])
cat( url, " " )
profile <- getURL(url)
row <- str_extract(profile, "Category.*?</tr>")
cell <- str_extract(row, "<td.*</td>" )
d$Category[i] <- str_replace_all( cell, "<.*?>", "" )
cat( d$Category[i], "\n" )
})
}
head(d)
stockSymbol
函数从纳斯达克网站检索股票列表(该网站还列出了其他几个交易所):如果您知道类似的网页列出了您想要的资产,您可以简单地检索和解析它。 - Vincent Zoonekynd