2017-08-26 1 views
0

J'utilise rvest pour gratter des données à partir des tables HTML d'un site Web interne. La couleur des lignes est significative, donc je veux extraire l'attribut BGCOLOR comme une colonne dans ma table finale, mais bien sûr html_table() extrait seulement le contenu.Comment inclure des attributs dans une table html Web-gratté

Voici ce que j'ai jusqu'ici. Un extrait de la table html est ci-dessous. Comment puis-je inclure une colonne pour la couleur?

html_nodes(samplepage,"table") 
tbl_content <- samplepage %>% 
    html_nodes("table") %>% 
    html_table(fill = TRUE, trim = TRUE) 
tbl_content 

<tr BGCOLOR = "#F8C0E0"> 
<td> BASOPHILS <td> microl  <td> 0.477 <td> 0.425 <td align="center"> 0.052 <td align="center"> 1.920 <td align="center"> 51.5 <td align="center"> 32 
</tr> 
<tr BGCOLOR = "#F8F0B0"> 
<td> CALCIUM <td > mg/dl  <td> 12.2 <td> 1.7 <td align="center"> 7.6 <td align="center"> 14.9 <td align="center"> 71 <td align="center"> 33 
</tr> 

Répondre

2

Vous pouvez construire votre propre analyseur pour remplacer html_table. purrr::map_df est pratique pour itérer sur les nœuds (tr s dans ce cas) et en combinant les résultats dans un data.frame:

library(rvest) 
library(tidyverse) 

html <- '<tr BGCOLOR = "#F8C0E0"> 
<td> BASOPHILS <td> microl  <td> 0.477 <td> 0.425 <td align="center"> 0.052 <td align="center"> 1.920 <td align="center"> 51.5 <td align="center"> 32 
</tr> 
<tr BGCOLOR = "#F8F0B0"> 
<td> CALCIUM <td > mg/dl  <td> 12.2 <td> 1.7 <td align="center"> 7.6 <td align="center"> 14.9 <td align="center"> 71 <td align="center"> 33 
</tr>' 

parsed_df <- html %>% 
    read_html() %>% 
    html_nodes('tr') %>% 
    map_df(~bind_cols(data_frame(bgcolor = html_attr(.x, 'bgcolor')), # grab attribute 
         # extract each row's values to 1-row data.frame 
         html_nodes(.x, 'td') %>% 
          html_text(trim = TRUE) %>% 
          set_names(paste0('x', seq_along(.))) %>% # or `%>% t() %>% as_data_frame()` 
          invoke(data_frame, .))) %>% 
    type_convert() # clean up types 

parsed_df 
#> # A tibble: 2 x 9 
#> bgcolor  x1  x2  x3 x4 x5 x6 x7 x8 
#>  <chr>  <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 
#> 1 #F8C0E0 BASOPHILS microl 0.477 0.425 0.052 1.92 51.5 32 
#> 2 #F8F0B0 CALCIUM mg/dl 12.200 1.700 7.600 14.90 71.0 33 

Plus simple mais moins souple, vous pouvez simplement tirer sur l'attribut puis fusionnez les résultats de html_table:

paste('<table>', html, '</table>') %>% # `html_table` needs a <table> tag 
    read_html() %>% 
    { 
     data.frame(bgcolor = html_nodes(., 'tr') %>% html_attr('bgcolor'), 
        html_table(.)) 
    } 
#> bgcolor  X1  X2  X3 X4 X5 X6 X7 X8 
#> 1 #F8C0E0 BASOPHILS microl 0.477 0.425 0.052 1.92 51.5 32 
#> 2 #F8F0B0 CALCIUM mg/dl 12.200 1.700 7.600 14.90 71.0 33 
+1

Terrific! J'ai utilisé la deuxième méthode, plus simple, et cela a fonctionné comme un charme. J'ai dû définir header = FALSE dans la fonction html_table(), cependant, pour les aligner correctement. Je vous remercie! – cricketbird