Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rvest: how to find all classes used in an HTML page?

I would like to find all classes used in the webpage below. Is this possible with rvest or will I need anyway some regex/grepl? I am able to scrape the info once I know the name of the class, but for pages with dynamically built class names it would be convenient to have an overview of the class es used.

library(rvest)

doc_url<-"http://curia.europa.eu/juris/document/document.jsf?text=&docid=160583&pageIndex=0&doclang=fr&mode=req&dir=&occ=first&part=1&cid=676771"

page<-read_html(doc_url)

language<- page%>%html_nodes(".C49FootnoteLangue")%>%html_text()
like image 650
Lod Avatar asked Dec 31 '15 15:12

Lod


1 Answers

Converting @hadley's comment to a CW answer, you can get a vector of all the classes by using the * wildcard.

Thus, the approach would look like:

page <- read_html(doc_url)

page %>% 
  html_nodes("*") %>% 
  html_attr("class") %>% 
  unique()
#  [1] NA                          "component"                 "waitBlock"
#  [4] "waitBlockContainer"        "toggle_img"                "btn_impression"
#  [7] "document_language"         "outputEcli"                "C19Centre"
# [10] "C71Indicateur"             "C02AlineaAltA"             "C72Alineadroite"
# [13] "C75Debutdesmotifs"         "C01PointnumeroteAltN"      "C04Titre1"
# [16] "C03Tiretlong"              "C05Titre2"                 "C06Titre3"
# [19] "C07Titre4"                 "C48DispositifIntroduction" "C08Dispositif"
# [22] "C77Signatures"             "C49FootnoteLangue"
like image 170
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 28 '22 17:09

A5C1D2H2I1M1N2O1R2T1