Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing onclick with rvest

Tags:

r

rvest

I have been struggling to get a piece of data using rvest. The piece of data I am looking for is the value 20960 which is insideOpenView(20960 ). How would I accomplish this with rvest?

An example section of the html I am working with is

<tr class="row-1" align="left">
<td style="width:120px;">
<a href="#" onclick='OpenView(20960 );return false;'>
BAKER, JAIME EDWARD</a>
</td>
</tr>
like image 392
thatsawinner Avatar asked Oct 31 '25 04:10

thatsawinner


1 Answers

I think this requires a little grepping...

library("rvest")
library("stringr")
read_html('<tr class="row-1" align="left">
<td style="width:120px;">
          <a href="#" onclick=\'OpenView(20960 );return false;\'>
          BAKER, JAIME EDWARD</a>
            </td>
            </tr>') %>% 
  html_nodes("a") %>% 
  html_attr("onclick") %>%
  str_extract("(?<=\\().*(?=\\))") %>%    # returns the stuff inside the parens
  str_trim(side="both")                   # trims whitespace from both sides
  [1] "20960"
like image 164
cory Avatar answered Nov 02 '25 18:11

cory