问题:在多个页面上抓取已停止工作

时间:2020-05-24 15:06:39

标签: r rvest

我正在从Tripadvisor刮刮旅馆的所有评论,并且我的代码导致以下错误:data.frame(textoComentario,fechaComentario)中的错误: 参数暗示不同的行数:6、5

我曾使用以下代码来刮刮另一家酒店,但该酒店仍然有效,但是我无法确定错误所在。我尝试使用其他CSS选择器,但没有任何效果。我能够完全运行一次代码,但是相同的评论又一次又一次地重复。我不知道如何解决该问题。 我附上我的代码,以便更轻松地了解问题

library(dplyr)
library(rvest)
#Link
web <- read_html("https://www.tripadvisor.es/Hotel_Review-g187499-d239247-Reviews-Melia_Girona-Girona_Province_of_Girona_Catalonia.html")
# Dataset to download the review sections
# 1. Texto comentarios
textoComentario<-web%>%
  html_nodes(".location-review-review-list-parts-ExpandableReview__reviewText--gOmRC span")%>%
  html_text()
textoComentario

# 2. Fecha comentario
fechaComentario<-web%>%
  html_nodes(".location-review-review-list-parts-EventDate__event_date--1epHa")%>%
  html_text()
fechaComentario <- strsplit(fechaComentario, ": ")
fechaComentario <- unlist(lapply(fechaComentario, FUN = function(x) {x[2]}))
fechaComentario


datos<-data.frame(textoComentario,fechaComentario)

# To go through all the review pages
for(i in 1:174){
  # 1. url

  url<-paste0("https://www.tripadvisor.es/Hotel_Review-g187499-d239247-Reviews-or",i*10,"-Melia_Girona-Girona_Province_of_Girona_Catalonia.htm")


  pagina<-read_html(url)


  textoComentario<-pagina%>%
    html_nodes(".location-review-review-list-parts-ExpandableReview__reviewText--gOmRC span")%>%
    html_text()
  textoComentario

  fechaComentario<-pagina%>%
    html_nodes(".location-review-review-list-parts-EventDate__event_date--1epHa")%>%
    html_text()
  fechaComentario <- strsplit(fechaComentario, ": ")
  fechaComentario <- unlist(lapply(fechaComentario, FUN = function(x) {x[2]}))
  fechaComentario



  nuevosDatos<-data.frame(textoComentario,fechaComentario)


  datos<-rbind(datos,nuevosDatos)

  print(paste0("Página ",i))
}

df<- datos

提前谢谢!

1 个答案:

答案 0 :(得分:0)

textoComentariofechaComentario的长度不同,因此无法按照您的方式组合在data.frame中。以下代码通过在将变量组合到数据帧之前将NAs添加到较短的变量来解决此问题:

library(dplyr)
library(rvest)

Link
web <- read_html("https://www.tripadvisor.es/Hotel_Review-g187499-d239247-Reviews-Melia_Girona-Girona_Province_of_Girona_Catalonia.html")
# Dataset to download the review sections
# 1. Texto comentarios
textoComentario<-web%>%
  html_nodes(".location-review-review-list-parts-ExpandableReview__reviewText--gOmRC span")%>%
  html_text()
textoComentario

# 2. Fecha comentario
fechaComentario<-web%>%
  html_nodes(".location-review-review-list-parts-EventDate__event_date--1epHa")%>%
  html_text()
fechaComentario <- strsplit(fechaComentario, ": ")
fechaComentario <- unlist(lapply(fechaComentario, FUN = function(x) {x[2]}))
fechaComentario


datos<-data.frame(textoComentario,fechaComentario)

# To go through all the review pages
for(i in 1:174){
  # 1. url

  url<-paste0("https://www.tripadvisor.es/Hotel_Review-g187499-d239247-Reviews-or",i*10,"-Melia_Girona-Girona_Province_of_Girona_Catalonia.htm")
  print(i)

  pagina<-read_html(url)


  textoComentario<-pagina%>%
    html_nodes(".location-review-review-list-parts-ExpandableReview__reviewText--gOmRC span")%>%
    html_text()


  fechaComentario<-pagina%>%
    html_nodes(".location-review-review-list-parts-EventDate__event_date--1epHa")%>%
    html_text()
  fechaComentario <- strsplit(fechaComentario, ": ")
  fechaComentario <- unlist(lapply(fechaComentario, FUN = function(x) {x[2]}))
  fechaComentario

  #make sure variables have equal length, if not add NAs to shorter variable
  if (length(textoComentario) < length(fechaComentario)) {textoComentario[length(textoComentario):length(fechaComentario)] <- NA}
  if (length(fechaComentario) < length(textoComentario)) {fechaComentario[length(fechaComentario):length(textoComentario)] <- NA}

  nuevosDatos<-data.frame(textoComentario,fechaComentario)

  datos<-rbind(datos,nuevosDatos)

  print(paste0("Página ",i))
}

这在NA中总共产生21个fechaComentario值:

df %>% 
  as_tibble %>% 
  filter(is.na(textoComentario) | is.na(fechaComentario))

# A tibble: 21 x 2
   textoComentario                                                                                                                                                      fechaComentario
   <fct>                                                                                                                                                                <fct>          
 1 "Empezaré por lo bueno. Solo el desayuno (buffet bastante completo) merece las cuatro estrellas, al margen del buen hacer del personal, muy atento, también en rece~ NA             
 2 "…"                                                                                                                                                                  NA             
 3 "Empezaré por lo bueno. Solo el desayuno (buffet bastante completo) merece las cuatro estrellas, al margen del buen hacer del personal, muy atento, también en rece~ NA             
 4 "…"                                                                                                                                                                  NA             
 5 "La estancia fue muy bien tanto en el trato en la recepción Como en en la cafetería . Creo que seria buena idea cambiar las bañeras por ducha , pues las bañeras so~ NA             
 6 "hotel necesita una reforma  Habitaciones e instalaciones antiguas  Desayuno bueno  Ubicación regular lejos del centro en un barrio nada bueno. Los botes de gel y ~ NA             
 7 "Es un hotel fantastico para realizar viajes de negocios y de placer. Desde la restauracion (camareros y camareras incluidas), pasando por recepcion y las habitaci~ NA             
 8 "El equipo Melià nos atendió en todo momento con la mejor de sus sonrisas, ayudándonos con todas las dudas que nos surgieron. El hotel tiene una situación muy buen~ NA             
 9 "Reserve una habitación doble estándar para una noche. Por la noche se escuchaba el ascensor, la habitación estaba pared con pared con él; no me di cuenta al dejar~ NA             
10 "…"                                                                                                                                                                  NA             
# ... with 11 more rows

这能解决您的问题吗?