오바마의 스펙 페이지를 워드 스크랩 등으로 만들기 위해 시도합니다. 1, 5, 10 개의 다른 페이지 (연설) , 루프 안에, 분리되어, 부호는 작동한다. 그러나 위의 루프를 만들면 결과 개체에 아무 것도 포함되지 않습니다 (NULL
).r (루프 포함)에서 웹 스크 레이 핑
누군가 나를 도울 수 있습니까? CSS 또는 XPath를 선택기의 지식이 조금 필요가있을 수 있지만
library(wordcloud)
library(tm)
library(XML)
library(RCurl)
site <- "http://obamaspeeches.com/"
url <- readLines(site)
h <- htmlTreeParse(file = url, asText = TRUE, useInternalNodes = TRUE,
encoding = "utf-8")
# getting the phrases that will form the web adresses for the speeches
teste <- data.frame(h[42:269, ])
teste2 <- teste[grep("href=", teste$h.42.269...), ]
teste2 <- as.data.frame(teste2)
teste3 <- gsub("^.*href=", "", teste2[, "teste2"])
teste3 <- as.data.frame(teste3)
teste4 <- gsub("^/", "", teste3[, "teste3"])
teste4 <- as.data.frame(teste4)
teste5 <- gsub(">.*$", "", teste4[, "teste4"])
teste5 <- as.data.frame(teste5)
# loop to read pages
l <- vector(mode = "list", length = nrow(teste5))
i <- 1
for (i in nrow(teste5)) {
site <- paste("http://obamaspeeches.com/", teste5[i, ], sep = "")
url <- readLines(site)
l[[i]] <- url
i <- i + 1
}
str(l)
제거'내가 <내가 1 '을 +와'에 루프를 변경하기위한 (전 1 : nrow (teste5))' – HubertL