0

I need to get data of the oil price table formed by javascript so that I can have it onto data.frame in R.

In the below codes, I am able to open the url in chrome browser using RSelenium but I am unable to extract the table of historical oil price in xpath [@id='historic-price-list']/div/div[2]/table. The below doesn't seem to give me a table or the values I wanted. https://markets.businessinsider.com/commodities/historical-prices/oil-price/usd?type=brent

library('RSelenium')
rD <- rsDriver(browser = "chrome") 
remDr <- rD[["client"]] #Start Chrome.
siteAdd <- "https://markets.businessinsider.com/commodities/historical-prices/oil-price/usd?type=brent"

remDr$navigate(siteAdd) #Open the site.

abc=remDr$findElement("css selector","//*[@id='historic-price-list']/div/div[2]/table > tbody > tr:nth-child(1) > th:nth-child(1)")$getElementText()

I hope to get it onto a table which I can put it onto data.frame.

Rei Cheong
  • 49
  • 3

1 Answers1

0

For any one who needs the codes! RSelenium codes more than some number of years cant really work and I was struggling how to do the exraction. Many thanks to this link too ! How to read an html table using Rselenium?

library('RSelenium')
library('XML')
rD <- rsDriver(browser = "chrome")
#Start Chrome.
remDr <- rD[["client"]]
#Add a test URL.
siteAdd <- "https://markets.businessinsider.com/commodities/historical-prices/oil-price/usd?type=brent"
#Open the site.
remDr$navigate(siteAdd)

doc <- htmlParse(remDr$getPageSource()[[1]])
readHTMLTable(doc)
Rei Cheong
  • 49
  • 3