Skip to content

Instantly share code, notes, and snippets.

@martinctc
Last active November 28, 2016 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinctc/d3d6a11683176c1abd46d3c2f0ba3a24 to your computer and use it in GitHub Desktop.
Save martinctc/d3d6a11683176c1abd46d3c2f0ba3a24 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment