ks123321
ks123321

Reputation: 61

Using rvest functions on a dataframe column

I have a large dataframe, called 'output', of which one column is html, called 'raw'.

I am trying to make a new column, style_col, which takes the style attribute of any element which has it. Here is my attempt:

style_col <- output %>% 
  mutate(style = read_html(raw) 
         %>% html_node('[style]') 
         %>% html_attr('style')) 

The part that extracts style from HTML works on a single piece of HTML, but I can't seem to get the syntax right for operating on a dataframe. What is the correct way of doing this? Thank you

Upvotes: 2

Views: 52

Answers (1)

Jakub.Novotny
Jakub.Novotny

Reputation: 3047

style_col <- output %>% 
  rowwise() %>% # I added this part
  # changed the formatting only here
  mutate(
    style = read_html(raw) %>%
      html_node('[style]') %>%
      html_attr('style')
    ) 

Upvotes: 3

Related Questions