martinctc
11/18/2016 - 3:07 PM

Scraper for tables on websites

Scraper for tables on websites

library("rvest")
url <- "https://bank.hangseng.com/1/2/rates/foreign-currency-tt-exchange-rates"

#Replace xpath with bits highlighting whole table on website using 'Inspect'

forex <- url %>%
  html() %>%
  html_nodes(xpath='//*[@id="viewns_7_0G3UNU10SD0MHTI7BJ91000000_:Display"]/div[1]/table') %>%
  html_table()
forex <- forex[[1]]

url2 <- "https://bank.hangseng.com/1/2/rates/gold-prices"

gold <- url2 %>%
  html() %>%
  html_nodes(xpath='//*[@id="viewns_7_0G3UNU10SD0MHTI7G5I0000000_:Display"]/div/table') %>%
  html_table()
gold<- gold[[1]]

timeform <- format(Sys.time(),"%Y-%m-%d,%H%M%S")
output_fx<-paste("Forex",timeform,".csv")
write.csv(forex,file=output_fx)

output_gold<-paste("Gold",timeform,".csv")
write.csv(gold,file=output_gold)