Scrape Yahoo Finance Financial Ratios

后端 未结 4 891
清歌不尽
清歌不尽 2021-02-04 19:52

I have been trying to scrap the value of the Current Ratio (as shown below) from Yahoo Finance using Beautiful Soup, but it keeps returning an empty value.

Inte

4条回答
  •  名媛妹妹
    2021-02-04 20:12

    Maybe this is not the answer you are looking for, but R can do this very easily and very quickly. See the link below.

    http://allthingsr.blogspot.com/2012/10/pull-yahoo-finance-key-statistics.html

    #######################################################################
    # Script to download key metrics for a set of stock tickers using the quantmod package
    #######################################################################
    require(quantmod)
    require("plyr")
    what_metrics <- yahooQF(c("Price/Sales", 
                              "P/E Ratio",
                              "Price/EPS Estimate Next Year",
                              "PEG Ratio",
                              "Dividend Yield", 
                              "Market Capitalization"))
    
    tickers <- c("AAPL", "FB", "GOOG", "HPQ", "IBM", "MSFT", "ORCL", "SAP")
    # Not all the metrics are returned by Yahoo.
    metrics <- getQuote(paste(tickers, sep="", collapse=";"), what=what_metrics)
    
    #Add tickers as the first column and remove the first column which had date stamps
    metrics <- data.frame(Symbol=tickers, metrics[,2:length(metrics)]) 
    
    #Change colnames
    colnames(metrics) <- c("Symbol", "Revenue Multiple", "Earnings Multiple", 
                           "Earnings Multiple (Forward)", "Price-to-Earnings-Growth", "Div Yield", "Market Cap")
    
    #Persist this to the csv file
    write.csv(metrics, "FinancialMetrics.csv", row.names=FALSE)
    
    #######################################################################
    
    #######################################################################
    ##Alternate method to download all key stats using XML and x_path - PREFERRED WAY
    #######################################################################
    
    setwd("C:/Users/i827456/Pictures/Blog/Oct-25")
    require(XML)
    require(plyr)
    getKeyStats_xpath <- function(symbol) {
      yahoo.URL <- "http://finance.yahoo.com/q/ks?s="
      html_text <- htmlParse(paste(yahoo.URL, symbol, sep = ""), encoding="UTF-8")
    
      #search for  nodes anywhere that have class 'yfnc_tablehead1'
      nodes <- getNodeSet(html_text, "/*//td[@class='yfnc_tablehead1']")
    
      if(length(nodes) > 0 ) {
       measures <- sapply(nodes, xmlValue)
    
       #Clean up the column name
       measures <- gsub(" *[0-9]*:", "", gsub(" \\(.*?\\)[0-9]*:","", measures))   
    
       #Remove dups
       dups <- which(duplicated(measures))
       #print(dups) 
       for(i in 1:length(dups)) 
         measures[dups[i]] = paste(measures[dups[i]], i, sep=" ")
    
       #use siblings function to get value
       values <- sapply(nodes, function(x)  xmlValue(getSibling(x)))
    
       df <- data.frame(t(values))
       colnames(df) <- measures
       return(df)
      } else {
        break
      }
    }
    
    tickers <- c("AAPL")
    stats <- ldply(tickers, getKeyStats_xpath)
    rownames(stats) <- tickers
    write.csv(t(stats), "FinancialStats_updated.csv",row.names=TRUE)  
    
    #######################################################################
    

提交回复
热议问题