'Portfolio dataset from investing.com

I'm new at web scraping an I'm stuck with this.

My goal is to generate a table with historical data from '2018-01-01' to '2021-12-31' but I have 0 idea of how to do it.

My code:

library(tidyquant)    # To download the data. Datos bursátiles.
library(plotly)       # To create interactive charts
library(timetk)       # To manipulate the data series
library(tidyr)        # Tidy format excel format
library(ggplot2)
library(forcats)      # Predictions
library(dplyr)
library(httr)
library(rvest)

url<-"https://es.investing.com/equities/home-depot-historical-data"

url%>%  
  GET(from = '2018-01-01',
      to = '2021-12-31')%>%
  http_status()

dolar_table<-url%>%
  read_html()%>%
  html_node('#curr_table', )%>%
  html_table()%>%
  tbl_df()

html_table

Serie<-dolar_table%>%
  rename('Cierre'='Último')%>%
  mutate(Fecha=(gsub('\\.','-',Fecha)),
         Apertura=gsub("\\.","",Apertura),
         Apertura=gsub(",",".",Apertura),
         Apertura=as.numeric(Apertura),
         Cierre=gsub("\\.","",Cierre),
         Cierre=gsub(",",".",Cierre),
         Cierre=as.numeric(Cierre))%>%
  select(c(Fecha,Apertura,Cierre))


Serie$Fecha<-lubridate::dmy(Serie$Fecha)
Serie

At least it does return a nice table but the dates are not the ones I need!

Also, I have to do this for more than 1 stock. I have a 5 stock portfolio and I don't know how to concatenate my stocks in the same dataset.

Hopefully someone knows how to procceed.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source