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

问题描述 投票:1回答:1

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

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

#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

谢谢您!

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

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)) }

© www.soinside.com 2019 - 2024. All rights reserved.