#' Scrape historical commodity prices
#'
#' @param URL A url from "https://uk.investing.com/commodities/".
#'
#' @return A dataframe of historical prices for the selected commodity
# @examples
# scrape_commodities(
# URL="https://uk.investing.com/commodities/gold-historical-data"
# )
# scrape_commodities(
# URL="https://uk.investing.com/commodities/silver-historical-data"
# )
#' @export
#' @importFrom xml2 read_html
#' @importFrom rvest html_nodes html_table
#' @importFrom lubridate parse_date_time
#function to scrape the historical price of commodities from uk.investing.com
scrape_commodities<-function(URL){
df<-(xml2::read_html(URL)%>%
rvest::html_nodes(css = "#curr_table")%>%
rvest::html_table())[[1]]
df$Date<-lubridate::parse_date_time(df$Date, orders = c("b", "dmy", "mdy"))
return(df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.