Search code examples
rrvestyahoo-finance

Pull Major Indices from Yahoo Finance


I would like to pull the Major World Indices Table from Yahoo Finance (this).

I have tried the following, but without success:

library(rvest)
yahoo_windizes <- "https://finance.yahoo.com/world-indices/"
read_html(yahoo_windizes) %>%
    html_nodes("table") %>% 
    html_table()

Or with the htmltab-library:

library(htmltab)
yahoo_windizes %>%
    htmltab(which = 1)

I just can't seem to get it to work. Help is much appreciated!


Solution

  • The last 3 visible columns are canvas elements. Whilst it may be possible to get these values it may be easier to pull them from elsewhere and assign them to your data object.

    In the meantime, if you want just the first 6 columns, you can re-construct the table in a number of ways, such as via a matrix:

    library(rvest)
    library(magrittr)
    library(purrr)
    
    yahoo_windizes <- "https://finance.yahoo.com/world-indices/"
    
    table <- read_html(yahoo_windizes) %>%
      html_node("#list-res-table table") 
    
    data <- map(1:6, function(x){ table %>% html_nodes(sprintf('td:nth-of-type(%i)', x)) %>% html_text()}) 
    
    data <- data %>% unlist() %>% matrix(nrow=length(data[[1]]) , ncol=6) %>% as.tibble()
    
    names(data) <- c('Symbol','Name','Last Price', 'Change', '%Change', 'Volume')