2015-12-29 11 views
5

Ich benutze rvest. Und ich möchte das Ergebnis zu einem Datenrahmen konvertieren:Konvertiere xml_nodeset in data.frame

> links <- pgsession %>% jump_to(urls[2]) %>% read_html() %>% html_nodes("a")  
> links 
{xml_nodeset (114)} 
[1] <a href="/Mitglieder/Detail/1213412">Date</a> 
[2] <a href="/Account/ChangePassword">Kennwort ändern</a> 
[3] <a href="/Account/BenutzernamenAendern/124312234">Benutzernamen ändern</a> 
[4] <a href="/Account/LogOff">Abmelden</a> 
...  

ich die folgende Methode verwendet:

library(plyr) 
ldply(xmlToList(links), data.frame) 
Error in UseMethod("xmlSApply") : 
    no applicable method for 'xmlSApply' applied to an object of class "xml_nodeset" 
df1 <- data.frame(character(13000)) 
df1 <- rbind(df1, data.frame(links))# append to data.frame 

Allerdings bekomme ich einen Fehler:

Error in UseMethod("xmlSApply") : 
    no applicable method for 'xmlSApply' applied to an object of class "xml_nodeset" 

Jeder Vorschlag, was Ich mache falsch?

Ich schätze Ihre Antworten!

+2

Was ist mit 'data.frame (hrefs = as (links," character "))'? – lukeA

Antwort

9

Dadurch erhalten Sie alle Attribute aus den Links in eine tbl_df. bind_rows bekommt man „füllen“ kostenlos:

library(rvest) 
library(dplyr) 

pg <- read_html("https://en.wikipedia.org/wiki/Main_Page") 
links <- html_nodes(pg, "a") 
bind_rows(lapply(xml_attrs(links), function(x) data.frame(as.list(x), stringsAsFactors=FALSE))) 

## Source: local data frame [310 x 10] 
## 
##  id       href     title class dir accesskey rel lang hreflang style 
## (chr)      (chr)     (chr) (chr) (chr)  (chr) (chr) (chr) (chr) (chr) 
## 1 top       NA      NA NA NA  NA NA NA  NA NA 
## 2  NA      #mw-head      NA NA NA  NA NA NA  NA NA 
## 3  NA     #p-search      NA NA NA  NA NA NA  NA NA 
## 4  NA    /wiki/Wikipedia    Wikipedia NA NA  NA NA NA  NA NA 
## 5  NA   /wiki/Free_content   Free content NA NA  NA NA NA  NA NA 
## 6  NA   /wiki/Encyclopedia   Encyclopedia NA NA  NA NA NA  NA NA 
## 7  NA /wiki/Wikipedia:Introduction Wikipedia:Introduction NA NA  NA NA NA  NA NA 
## 8  NA  /wiki/Special:Statistics  Special:Statistics NA NA  NA NA NA  NA NA 
## 9  NA  /wiki/English_language  English language NA NA  NA NA NA  NA NA 
## 10 NA   /wiki/Portal:Arts   Portal:Arts NA NA  NA NA NA  NA NA 
## .. ...       ...     ... ... ...  ... ... ...  ... ... 

Alternativ Sie purrr verwenden:

library(rvest) 
library(purrr) 

pg <- read_html("https://en.wikipedia.org/wiki/Main_Page") 
html_nodes(pg, "a") %>% 
    map(xml_attrs) %>% 
    map_df(~as.list(.)) 

## # A tibble: 342 × 10 
##  id       href     title class dir accesskey rel hreflang lang style 
## <chr>      <chr>     <chr> <chr> <chr>  <chr> <chr> <chr> <chr> <chr> 
## 1 top       <NA>     <NA> <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## 2 <NA>      #mw-head     <NA> <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## 3 <NA>     #p-search     <NA> <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## 4 <NA>    /wiki/Wikipedia    Wikipedia <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## 5 <NA>   /wiki/Free_content   Free content <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## 6 <NA>   /wiki/Encyclopedia   Encyclopedia <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## 7 <NA> /wiki/Wikipedia:Introduction Wikipedia:Introduction <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## 8 <NA>  /wiki/Special:Statistics  Special:Statistics <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## 9 <NA>  /wiki/English_language  English language <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## 10 <NA>   /wiki/Portal:Arts   Portal:Arts <NA> <NA>  <NA> <NA>  <NA> <NA> <NA> 
## # ... with 332 more rows 

die ich denke, ist funktionell idiomatischen und insgesamt sauberer Ansatz.

+0

Mein Anwendungsfall ist, dass ich weitere Seiten mit den gleichen Ergebnissen habe und sie dem Datenrahmen hinzufügen möchte. Wie könnte ich 'bind_rows' ähnlich verwenden? – mrquad

+0

'bind_rows' wird diese auch" füllen ". – hrbrmstr