У меня проблемы с очисткой пакета rvest в r. Я попытался собрать информацию с веб-сайта и создать фрейм данных, содержащий векторы, указанные внутри l oop.
Если я не использую функцию l oop, я получаю правильные данные. Кто-нибудь может мне сказать, что не так со следующим кодом?
Я догадываюсь, что мне не удалось объединить векторы ...
library(rvest)
library(dplyr)
library(tidyr)
library(stringr)
library(stringi)
#This is the URL from which I would like to get information.
source_url <- "https://go2senkyo.com/local/senkyo/"
senkyo <- data.frame()
*start for loop
for (i in 50:60) {
target_page <- paste0(source_url, i)
recall_html <- read_html(source_url, encoding = "UTF-8")
prefecture <- recall_html %>%
html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "column_ttl_small", " " ))]') %>%
html_text()
city <- recall_html %>%
html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "column_ttl", " " ))]') %>%
html_text()
city <- trimws(gsub("[\r\n]", "", city )) %>% unlist() %>% str
candidate <- recall_html %>%
html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "m_senkyo_result_table", " " ))]') %>%
html_text()
candidate <- trimws(gsub("[\r\n\t]", "", candidate ))
all <- recall_html %>%
html_nodes(xpath='//td') %>%
html_text()
all <- trimws(gsub("[\r\n\t]", "", all))
election_day <- all[1]
turnout <- all[2]
magnitude_candidates <- all[3]
notificationday <- all[4]
turnout_lasttime <- all[5]
others <- all[6]
senkyo2 <- cbind(prefecture, city, candidate, election_day, turnout, magnitude_candidates, notificationday,
turnout_lasttime, others)
senkyo <- rbind(senkyo , senkyo2)
}