Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read an html table using Rselenium?

I'm using Rselenium to navigate to a webpage. The following code is doing so. I haven't provided the url because I'm using the url in a company which needs vpn to connect:

RSelenium::startServer()
require(RSelenium)
remDr <- remoteDriver()
remDr$navigate("some url")

After I navigate to the webpage, inside the html source I have the following table:

<font size="2">
<table border="1">
<tbody>
<tr>
<td> item1 </td>
<td> 0 </td>
<td> 0.05 </td>
<td> 2.43 </td>
<td align="center"> Pct </td>
<td align="center"> 1 </td>
</tr>
</tbody>
</table>

Now the question is how can I pull out the content of this table? Please assume the url is not existent, otherwise I can use an XML function: readHTMLTable(remDr$getCurrentUrl()). But this does not work for some reason. I need to use the remoteDriver handle (remDr) only. Thanks so much for your time

like image 913
mamal Avatar asked Apr 29 '15 00:04

mamal


2 Answers

I prefer using rvest, so what I did was:

# Importing libraries
library(RSelenium)
library(rvest)

# Extracting table
remDr$getPageSource()[[1]] %>% 
  read_html() %>%
  html_table()
like image 70
Esben Eickhardt Avatar answered Oct 31 '22 13:10

Esben Eickhardt


Something like:

library(XML)
doc <- htmlParse(remDr$getPageSource()[[1]])
readHTMLTable(doc)

should allow you to access the html and process the tables contained.

like image 41
jdharrison Avatar answered Oct 31 '22 15:10

jdharrison