Quiero recopilar comentarios del foro al data.frame. Quiero recopilar todos los comentarios de una URL a una fila. El número de comentarios no será igual (comentarios máximos = 3) así que quiero obtener "NA" en cualquier celda sin comentarios.
library(rvest)
library(xml2)
library(qdapRegex)
url_page <- c("http://medyczka.pl/forum-gastrologiczne/")
web <- read_html(url_page)
post_url <- web %>% html_nodes(css='.title') %>%
html_attr('href') %>%
as.character()
post_url <-data.frame(post_url)
#Prepare df for all possible commentaries
posts_all <-data.frame()
#Let' make a simple function
for(i in 1:5){
web2<-read_html(as.character(post_url[i,1]))
posts <- web2 %>% html_nodes(css='.restore') %>%
html_text() %>%
as.character()
posts <- rm_between(posts,'\r','\t', replacement="")
posts_df <-data.frame(posts)
posts_all <-rbind(posts_all,posts_df)
}
str(posts_all)
Sin embargo, obtuve 1 columna con 92 filas, en lugar de 5 filas hasta 30 comentarios (columnas).
#> str(posts_all)
#'data.frame': 92 obs. of 1 variable:
#$ posts: Factor w/ 89 levels "Do tego potrzeba wiedzy a tu nie ma lekarzy. Radzę zapytać na innym forum medycznym.",..: 14 4 11 3 1 6 12 2 7 10 ...
¿Qué hice mal? ¿Cómo recopilar correctamente los datos?
Te sugiero que investigues tidyverse
junto con rvest
. Este debería ser un buen comienzo para ti;
library(tidyverse)
library(rvest)
pg <- read_html('http://medyczka.pl/forum-gastrologiczne/')
tbl <- tibble(
title = pg %>% html_nodes('.title') %>% html_text(),
title_link = pg %>% html_nodes('.title') %>% html_attr('href')
)
get_comment <- function(url) {
pg <- read_html(url)
tbl <- tibble( posts = pg %>% html_nodes('.restore') %>% html_text() %>% str_trim(),
link = url
) %>% mutate(post_seq = seq_along(posts))
}
posts_all <- NULL
posts_all <- map(tbl$title_link, get_comment) %>% bind_rows()
tbl_posts_all <- tbl %>% left_join(posts_all, by = c('title_link' = 'link'))
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras