我以前有以下代码,效果很好,它使用quantmod
包从Google下载财务信息。但是此功能此后已停止工作。值得庆幸的是,有人已经从Yahoo here获取数据。
library(tidyquant)
#symbols <- c("HOG", "GOOG", "GE")
stockinfo <- tq_index("SP500")
symbols <- stockinfo$symbol
library(quantmod)
symbols.f <- sapply(symbols, function(x) { paste0(x, ".f") })
symbols <- sub("\\.f","", symbols.f)
tickers <- new.env()
lapply(symbols, getFinancials, env=tickers)
BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))
我正在应用以下代码,其中包含从yahoo下载财务信息的功能;
#########################################################
#Set the function to scrape Yahoo finance
getFin <- function(stock){
if ("rvest" %in% installed.packages()) {
library(rvest)
}else{
install.packages("rvest")
library(rvest)
}
for (i in 1:length(stock)) {
tryCatch(
{
url <- "https://finance.yahoo.com/quote/"
url <- paste0(url,stock[i],"/financials?p=",stock[i])
wahis.session <- html_session(url)
p <- wahis.session %>%
html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
html_table(fill = TRUE)
IS <- p[[1]]
colnames(IS) <- paste(IS[1,])
IS <- IS[-c(1,5,12,20,25),]
names_row <- paste(IS[,1])
IS <- IS[,-1]
IS <- apply(IS,2,function(x){gsub(",","",x)})
IS <- as.data.frame(apply(IS,2,as.numeric))
rownames(IS) <- paste(names_row)
temp1 <- IS
url <- "https://finance.yahoo.com/quote/"
url <- paste0(url,stock[i],"/balance-sheet?p=",stock[i])
wahis.session <- html_session(url)
p <- wahis.session %>%
html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
html_table(fill = TRUE)
BS <- p[[1]]
colnames(BS) <- BS[1,]
BS <- BS[-c(1,2,17,28),]
names_row <- BS[,1]
BS <- BS[,-1]
BS <- apply(BS,2,function(x){gsub(",","",x)})
BS <- as.data.frame(apply(BS,2,as.numeric))
rownames(BS) <- paste(names_row)
temp2 <- BS
url <- "https://finance.yahoo.com/quote/"
url <- paste0(url,stock[i],"/cash-flow?p=",stock[i])
wahis.session <- html_session(url)
p <- wahis.session %>%
html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
html_table(fill = TRUE)
CF <- p[[1]]
colnames(CF) <- CF[1,]
CF <- CF[-c(1,3,11,16),]
names_row <- CF[,1]
CF <- CF[,-1]
CF <- apply(CF,2,function(x){gsub(",","",x)})
CF <- as.data.frame(apply(CF,2,as.numeric))
rownames(CF) <- paste(names_row)
temp3 <- CF
assign(paste0(stock[i],'.f'),value = list(IS = temp1,BS = temp2,CF = temp3),envir = parent.frame())
},
error = function(cond){
message(stock[i], "Give error ",cond)
}
)
}
}
################## END OF FUNCTION ######################
#
#########################################################
symbols <- c("HOG", "GOOG", "GE")
我可以使用以下getFin(symbols)
下载财务报表。但是我遇到了一个问题,因为我想使用以前的数据;
tickers <- new.env()
lapply(symbols, getFin, env=tickers)
BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))
我在lapply(symbols, getFin, env=tickers)
遇到错误,但不太确定为什么,因为我将数据以与以前相同的格式输入。
编辑:
大致了解我要实现的目标(作为最终目标)。我想要3个分别用于BS
-资产负债表,IS
-损益表和CF
-现金流量`的数据框,每个公司列为一列。