我在用rvest
.我想将结果转换为数据框:
> links <- pgsession %>% jump_to(urls[2]) %>% read_html() %>% html_nodes("a") > links {xml_nodeset (114)} [1] Date [2] Kennwort ändern [3] Benutzernamen ändern [4] Abmelden ...
我使用以下方法:
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"
有什么建议我做错了吗?
感谢您的回复!
这将从链接中获取所有属性tbl_df
.bind_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 #### 1 top ## 2 #mw-head ## 3 #p-search ## 4 /wiki/Wikipedia Wikipedia ## 5 /wiki/Free_content Free content ## 6 /wiki/Encyclopedia Encyclopedia ## 7 /wiki/Wikipedia:Introduction Wikipedia:Introduction ## 8 /wiki/Special:Statistics Special:Statistics ## 9 /wiki/English_language English language ## 10 /wiki/Portal:Arts Portal:Arts ## # ... with 332 more rows
我认为这更具功能性和整体清洁方法.