【问题标题】:How to get Financial Information of Stocks in R如何在 R 中获取股票的财务信息
【发布时间】:2018-05-15 16:27:03
【问题描述】:

我正在尝试获取资产净值、股息、负债等财务信息。我尝试使用quantmod 包获取所有股票信息。但它不适用于financials。有什么方法可以在 R 中获取财务数据。我使用了下面的代码,它给了我错误。

代码

library(quantmod)
statements <- getFinancials("AAPL", auto.assign=FALSE)

错误:

Error: ‘getFinancials.google’ is defunct.
Google Finance stopped providing data in March, 2018.

还有其他方法可以获取财务信息吗?

【问题讨论】:

  • 由于您在这里似乎并没有真正提出编程问题,只是寻找不同的数据源,也许您的问题更适合quant.stackexchange.com

标签: r finance quantmod stock


【解决方案1】:

也许以下内容对你有用:

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)
      }
    )
  }
}

那么简单

symbols <- c("NVDA", "GOOG", "GE")
getFin(symbols)

GOOG.f$IS
GOOG.f$BS
GOOG.f$CF

返回;

> head(GOOG.f$CF)
                                12/31/2017
Net Income                        12662000
Depreciation                       6899000
Adjustments To Net Income          8284000
Changes In Accounts Receivables   -3768000
Changes In Liabilities             1121000
Changes In Inventories                  NA
                                12/31/2016
Net Income                        19478000
Depreciation                       6100000
Adjustments To Net Income          7158000
Changes In Accounts Receivables   -2578000
Changes In Liabilities              333000
Changes In Inventories                  NA
                                12/31/2015
Net Income                        16348000
Depreciation                       5024000
Adjustments To Net Income          5609000
Changes In Accounts Receivables   -2094000
Changes In Liabilities              246000
Changes In Inventories                  NA
                                12/31/2014
Net Income                        14136000
Depreciation                       4601000
Adjustments To Net Income          3615000
Changes In Accounts Receivables   -1641000
Changes In Liabilities              261000
Changes In Inventories                  NA

感谢Everton Reis

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多