Reading a Table from Yahoo Finance

December 5, 2016
By

(This article was first published on Copula, and kindly contributed to R-bloggers)

As you know, the composition of an Index changes over time and this scipt takes the Yahoo Finance page and retrieves all individual stock symbols for you and stores it in the tickers object.

I had to update my R-Version to use the RCurl package – this is required as it is a https:// connection we would like to use.

I was surprised how easy it is to parse the xml page and retrieve all the tables on the page.

In this little example we are interested in retrieving all symbols of the DAX (30 largest listed companies in Germany) from this link https://de.finance.yahoo.com/q/cp?s=^GDAXI

It is the table 5 which holds the relevant data for us  – it is completely transparent to the user and I enjoyed using the XML – Package.

library(XML)
library(RCurl)

universum <-function()
{
  world=c(“^GDAXI”,”^MDAXI”,”^SDAXI”,”^TECDAX”)
  my=NA
  for(i in 1:NROW(world)) {
    url=paste(“https://de.finance.yahoo.com/q/cp?s=”,world[[i]],sep = “”)
    s <- getURL(url)  #rcurl package
    t=readHTMLTable(s)
    t=t[[5]]
    t = t[-(1:5), ]
    newtickers=t[,1]
    a=NROW(my)
    b=NROW(newtickers)
    my[a+1:b]=as.character(newtickers)
   
  }
  my = my[-1]
  tickers=my
  rm(t)
  rm(a)
  rm(b)
  rm(i)
  rm(my)
  rm(newtickers)
  return(tickers)
}

To leave a comment for the author, please follow the link and comment on their blog: Copula.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)