【问题标题】:Rvest - Error in UseMethod("read_xml") : no applicable method for 'read_xml' applied to an object of class "factor"Rvest - UseMethod(“read_xml”)中的错误:没有适用于“read_xml”的方法应用于“因子”类的对象
【发布时间】: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,你不知道上面的错误吗?

标签: r rvest


【解决方案1】:

实际上,我看到这两种方法都有效,并进行了一些调整。第一个问题是在初始代码 [html_nodes("H1")] 中显示的是“H1”而不是真正的“h1”。所以必须纠正这个问题。

第二个问题与错误类型的“URL”有关。即,它具有以下属性:

> typeof(URLs)
[1] "list"
> length(URLs)
[1] 1

同时,我希望它是:

> typeof(URLs)
[1] "character"
> length(URLs)
[1] 10 #or some other number

因此我做了以下解决方法:

n<-nrow(URLs)
URLs2<-character()
for (i in 1:n) {
  URLs2[i]<-as.character(URLs[i,1])
}

这使得初始版本可以正常工作。这是完整的代码(顺便说一句,谢谢你提供了一个很好的例子):

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) 

oversdf <- data.frame(URLs=URLs)

rownames(oversdf)  #returns a vector of row names in the overs data.frame:

URLs <-subset(oversdf, grepl("pmatch", oversdf$URLs),stringsAsFactors       =       FALSE)

write.csv(URLs,file=paste(getwd(),"/sportURLs.csv",sep=""),row.names=FALSE)

Catcher1 <- data.frame(FMatch=character(),TotalGoals=character    (),stringsAsFactors = FALSE)

##################################
#start of workaround
n<-nrow(URLs)
URLs2<-character()
for (i in 1:n) {
  URLs2[i]<-as.character(URLs[i,1])
}

#Start of for loop

for (i in URLs2) {

#end of workaround
#######################################

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("*")

}

返回:

> Catcher1
                               FMatch TotalGoals
1      Dep. Espanol vs Comunicaciones       2.22
2          San Martín B. vs Ituzaingó       1.77
3      Leandro N. Alem vs Def. Unidos       2.03
4     Dep. Laferre vs Central Córdoba       2.44
5     J.J. Urquiza vs Sport. Italiano       2.53
6       Excursionistas vs Berazategui       2.56
7                 Dock Sud vs Midland       1.74
8               Dep. Armenio vs Luján       1.47
#and so on

【讨论】:

    【解决方案2】:

    基本上问题在于在每个循环中设置 HTML 环境。为了解决这个问题,我在每个循环的开头使用了html_session(),并将其提供给html_nodes()

    for (i in URLs) {
    
      WS1 <- html_session(i)
      FMatch <- html_nodes(WS1, "h1") %>% html_text() %>% as.character()
      TotalGoals <- html_nodes(WS1, ".trow3+ .trow2 td~ td+ td font b") %>% html_text() %>% as.character()
      temp <- data.frame(FMatch,TotalGoals)
      Catcher1 <- rbind(Catcher1,temp)
      cat("*")
    }
    

    返回:

    R> Catcher1
                           FMatch TotalGoals
    1      Santa Cruz vs Criciuma       2.07
    2         FC Kiffen vs FC KTP       3.08
    3       Furth B vs Augsburg B       2.00
    4          Vikingur R. vs IBV       3.67
    5  IA Akranes vs KR Reykjavik       4.00
    6      Hafnarfjordur vs Valur       2.84
    7        Valerenga B vs Skeid       2.25
    8      Constanta vs Voluntari       1.75
    9         Syrianska vs Norrby       3.46
    10          Osters IF vs GAIS       3.14
    11 Sleipner vs Linkoping City       2.94
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-05
      • 2019-09-12
      • 2022-12-15
      • 2023-01-22
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多