Scrapping ibex35 invertia.com y limpieza de datos

Carga de datos

library(XML)
library(lubridate)

Url

url <- 'http://www.invertia.com/mercados/bolsa/indices/ibex-35/acciones-ib011ibex35/1a'

Carga HTML de la url

HTMLpage <- htmlTreeParse(url, useInternalNodes = T, encoding = "UTF-8")

Seleccionamos el nodo de la tabla de cotizaciones - clase HTML es “tb_fichas”

table_node <- getNodeSet(HTMLpage, path = '//table[@class="tb_fichas"]')

Cargamos la tabla de cotizaciones

ibex <- readHTMLTable(table_node[[1]], header = TRUE, as.data.frame = TRUE, stringsAsFactors = FALSE)

Limpieza de datos

Eliminamos la columna 5 de barras del div. % y cambiamos los names del data.frame

ibex[,5] <- NULL
names(ibex) <- c('Ticker', 'Ultimo', 'Dif', 'Dif_Porcentaje', 'Max',
                 'Min', 'Volumen', 'Capital', 'Yield_Porcentaje', 'PER', 'Fecha_Hora')

Ponemos NA en los valores n.a y n.d en caso de que los haya

ibex[,2:ncol(ibex)] <- sapply(ibex[,2:ncol(ibex)], gsub, pattern='n\\.[a|d]', replacement = NA)

Eliminamos % del Yield

ibex$Yield_Porcentaje <- gsub(ibex$Yield_Porcentaje, pattern = '%', replacement = '')

Cambiamos comas por puntos en los valores numéricos (excepto volumen que es un integer largo)

valores_numericos <- !(colnames(ibex) %in% c('Ticker', 'Volumen', 'Fecha_Hora'))
ibex[, valores_numericos] <- sapply(ibex[, valores_numericos], gsub, pattern = ',', replacement = '.')

Cambiamos los tipos de los valores numéricos

ibex[, valores_numericos] <- sapply(ibex[, valores_numericos], as.double)

Eliminamos los puntos de Volumen y cambiamos el tipo a integer

ibex$Volumen <- as.integer(gsub(ibex$Volumen, pattern = '\\.', replacement = ''))

Obtenemos la fecha (Suponemos la fecha del PC local) para determinar si estamos en mercado o no

Los horarios del mercado español son: L-V De 9:00 h a 17:30 h

fecha_hoy <- ymd_hms(Sys.time(), tz = "Europe/Madrid")

inicio_cotizacion <- fecha_hoy
hour(inicio_cotizacion) <- 9
minute(inicio_cotizacion) <- 0
second(inicio_cotizacion) <- 0

fin_cotizacion <- fecha_hoy
hour(fin_cotizacion) <- 17
minute(fin_cotizacion) <- 30
second(fin_cotizacion) <- 0

Condición para saber si está o no en mercado

la función wday considera el día 1 como Domingo

if (wday(fecha_hoy) %in% c(2:6)){
  if (fecha_hoy  %within% interval(inicio_cotizacion, fin_cotizacion)){
    #En mercado - entre semana - Convertimos la fecha a formato dd/mm/aaaa hh:mm
    fecha_hoy <- format(fecha_hoy, "%d/%m/%Y")
    ibex$Fecha_Hora <- sapply(fecha_hoy, paste, ibex$Fecha, sep = ' ')
    
  } else { 
    #Fuera mercado - entre semana - Convertimos la fecha a formato dd/mm/aaaa   
    ibex$Fecha_Hora <- format(fecha_hoy, "%d/%m/%Y")
  }
  
  #Fuera mercado - fin de semana - dejamos la columna tal y como viene con formato dd/mm/aaaa

}

Ibex 35

ibex
##    Ticker Ultimo    Dif Dif_Porcentaje    Max    Min  Volumen Capital
## 1    BKIA  1.362  0.064           4.93  1.375  1.333 49493356  15.687
## 2    SCYR  4.148  0.107           2.65  4.150  4.063  4254587   2.083
## 3     OHL 21.780  0.335           1.56 21.930 21.210  1015026   2.172
## 4     MAP  3.170  0.031           0.99  3.171  3.140  1460745   9.762
## 5     ABG  3.067  0.030           0.99  3.068  3.010  2639333      NA
## 6     FCC 10.595  0.100           0.95 10.635 10.505   650079   2.761
## 7     ITX 28.335  0.250           0.89 28.400 28.005   343501  88.310
## 8     ACS 33.490  0.270           0.81 33.490 32.955   197002  10.538
## 9     BKT  7.045  0.056           0.80  7.090  7.002  1143457   6.333
## 10    GAM 10.680  0.085           0.80 10.690 10.510   574375   2.983
## 11    TRE 37.280  0.240           0.65 37.380 37.070    79066   2.084
## 12    MTS  9.834  0.057           0.58  9.870  9.800   302257  16.377
## 13    IBE  6.141  0.034           0.56  6.147  6.083  2139845  39.232
## 14    ELE 18.165  0.095           0.53 18.195 18.080  1097460  19.232
## 15    ANA 70.390  0.370           0.53 70.500 70.000    17636   4.030
## 16    SAN  6.567  0.034           0.52  6.590  6.534 10329510  92.336
## 17    DIA  6.834  0.031           0.46  6.860  6.771   781962   4.449
## 18    POP  4.133  0.019           0.46  4.159  4.120  4281964   8.729
## 19    FER 19.010  0.070           0.37 19.015 18.930   510619  13.944
## 20    TEF 13.940  0.045           0.32 13.990 13.925  4439018  64.921
## 21   BBVA  9.008  0.028           0.31  9.050  8.964  9683065  56.074
## 22    REE 76.360  0.220           0.29 76.460 75.620   138494  10.329
## 23    REP 17.325  0.050           0.29 17.380 17.215  1219047  23.817
## 24    SAB  2.461  0.006           0.24  2.488  2.455  5452707   9.905
## 25    AMS 36.920  0.070           0.19 37.340 36.665   320960  16.525
## 26    BME 39.010  0.050           0.13 39.140 38.900   144822   3.262
## 27   CABK  4.154  0.004           0.10  4.180  4.143  5620410  23.740
## 28    JAZ 12.505  0.005           0.04 12.525 12.480   151303   3.208
## 29    GAS 21.590 -0.005          -0.02 21.680 21.410   354390  21.605
## 30   ICAG  7.981 -0.016          -0.20  8.052  7.850  1112896  11.802
## 31    ENG 27.310 -0.110          -0.40 27.490 27.180   306114   6.520
## 32    TL5 10.975 -0.065          -0.59 11.075 10.910   484029   4.465
## 33    ABE 17.320 -0.130          -0.74 17.535 17.305   292517  15.559
## 34    GRF 35.975 -0.280          -0.77 36.365 35.560   199856  11.504
## 35    IDR  8.900 -0.620          -6.51  9.281  8.850  3324944   1.461
##    Yield_Porcentaje   PER       02/03/2015
## 1                NA 12.53 02/03/2015 11:53
## 2                NA 14.43 02/03/2015 11:53
## 3              3.11  7.37 02/03/2015 11:53
## 4              4.42 10.03 02/03/2015 11:53
## 5                NA    NA 02/03/2015 11:53
## 6                NA 24.64 02/03/2015 11:53
## 7              1.71 31.14 02/03/2015 11:52
## 8              3.45 13.95 02/03/2015 11:53
## 9              1.10 16.38 02/03/2015 11:52
## 10               NA 19.07 02/03/2015 11:53
## 11             3.74 14.22 02/03/2015 11:51
## 12             1.25 17.88 02/03/2015 11:53
## 13             4.41 16.35 02/03/2015 11:53
## 14            78.03 18.45 02/03/2015 11:53
## 15               NA 27.04 02/03/2015 11:51
## 16             9.12 12.14 02/03/2015 11:53
## 17             2.34 17.09 02/03/2015 11:53
## 18             1.21 17.97 02/03/2015 11:53
## 19             3.53 17.88 02/03/2015 11:53
## 20             5.28 16.21 02/03/2015 12:08
## 21             4.53 14.30 02/03/2015 11:53
## 22             3.47 16.93 02/03/2015 11:53
## 23            11.30 17.64 02/03/2015 11:53
## 24             1.22 17.09 02/03/2015 11:53
## 25             1.75 22.17 02/03/2015 11:53
## 26             4.23 18.40 02/03/2015 11:53
## 27             4.81 16.31 02/03/2015 11:53
## 28               NA 33.02 02/03/2015 11:52
## 29             4.17 14.39 02/03/2015 11:53
## 30               NA 12.69 02/03/2015 11:53
## 31             4.69 15.92 02/03/2015 11:53
## 32               NA 23.35 02/03/2015 11:53
## 33             3.81 20.53 02/03/2015 11:53
## 34             1.26 20.56 02/03/2015 11:52
## 35             3.82 11.82 02/03/2015 11:53