【问题标题】:Convert xml_nodeset to data.frame将 xml_nodeset 转换为 data.frame
【发布时间】:2015-12-29 13:15:39
【问题描述】:

我正在使用rvest。我想将结果转换为数据框:

> 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>
...    

我使用了以下方法:

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

但是,我收到一个错误:

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

任何建议我做错了什么?

感谢您的回复!

【问题讨论】:

  • data.frame(hrefs=as(links, "character")) 呢?

标签: r statistics


【解决方案1】:

这会将链接中的所有属性转换为tbl_dfbind_rows 让你免费“填充”:

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
## ..   ...                          ...                    ...   ...   ...       ...   ...   ...      ...   ...

或者,您可以使用purrr:

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

我认为这在功能上更惯用,整体上更简洁。

【讨论】:

  • 我的用例是,我有更多具有相同结果的页面,并希望将它们添加到数据框中。我怎么能类似地使用bind_rows
  • bind_rows 也会“填充”这些内容。
  • 用最新版本的rvest,函数是html_attrs和xml_attrs
【解决方案2】:

试试这个,它对我有用: 该函数由https://rdrr.io/github/lawine90/datagokR/src/R/xml_to_dataframe.R编写

只需替换 link 即可:)

library(xml2)

xml_to_dataframe <- function(nodeset){
  if(class(nodeset) != 'xml_nodeset'){
    stop('Input should be "xml_nodeset" class')
  }
  lst <- lapply(nodeset, function(x){
    tmp <- xml2::xml_text(xml2::xml_children(x))
    names(tmp) <- xml2::xml_name(xml2::xml_children(x))
    return(as.list(tmp))
  })
  result <- do.call(plyr::rbind.fill, lapply(lst, function(x)
    as.data.frame(x, stringsAsFactors = F)))
  return(dplyr::as.tbl(result))
}

#list of files to process

sitemap_raw <-"your-link"

sitemap <-read_xml(sitemap_raw)
sitemap_nodeset <- xml_children(sitemap)

sitemap_df <- xml_to_dataframe(sitemap_nodeset)

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2018-06-20
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
相关资源
最近更新 更多