【发布时间】:2017-12-01 10:58:05
【问题描述】:
我正在使用以下 rvest 代码:
library(rvest)
URL <- "http://www.soccerstats.com/matches.asp" #Feed page
WS <- read_html (URL) #reads webpage into WS variable
URLs <- WS %>% html_nodes ("a:nth-child(1)") %>% html_attr("href") %>% as.character() # Get the CSS nodes & extract the URLs
URLs <- paste0("http://www.soccerstats.com/",URLs)
grepl("pmatch", oversdf$URLs)
URLs <-subset(oversdf, grepl("pmatch", oversdf$URLs),stringsAsFactors = FALSE)
Catcher1 <- data.frame(FMatch=character(),TotalGoals=character (),stringsAsFactors = FALSE)
#Start of for loop
for (i in URLs) {
WS1 <- read_html(i)
FMatch <- WS1 %>% html_nodes("H1") %>% html_text() %>% as.character()
TotalGoals <- WS1 %>% html_nodes(".trow3+ .trow2 td~ td+ td font b") %>% html_text() %>% as.character()
temp <- data.frame(FMatch,TotalGoals)
Catcher1 <- rbind(Catcher1,temp)
cat("*")
}
当它尝试运行循环时出现错误:
UseMethod("read_xml") 中的错误:没有适用于 'read_xml' 的方法应用于“因子”类的对象
查看论坛帖子我需要使用 stringsAsFactors = FALSE,因为我的数据框会将字段数据存储为因子而不是字符串。
我能想到的唯一可以去的地方是临时 df:
temp
但是我尝试将它应用于上面的 df it 语法错误,有什么想法吗?
(显然我是新手,所以我可能对上面的错误是什么导致了错误,就像我读过的各种论坛帖子一样)
干杯
【问题讨论】:
-
任何人有什么想法吗?
-
嗨@anotherfred,你不知道上面的错误吗?