Site icon R-bloggers

Reading a Table from Yahoo Finance

[This article was first published on Copula, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.