R/scrape_commodities.r

Defines functions scrape_commodities

Documented in scrape_commodities

#' 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)

}
lina2497/webscrapeR documentation built on Nov. 15, 2019, 1:48 p.m.