我想刮我的項目的某些天文學相關定義的維基。代碼工作得很好,但我無法避免404s。我試過tryCatch
。我想我在這裏錯過了一些東西。刮一個循環,並避免404錯誤
我正在尋找一種在運行循環時克服404s的方法。這裏是我的代碼:
library(rvest)
library(httr)
library(XML)
library(tm)
topic<-c("Neutron star", "Black hole", "sagittarius A")
for(i in topic){
site<- paste("https://en.wikipedia.org/wiki/", i)
site <- read_html(site)
stats<- xmlValue(getNodeSet(htmlParse(site),"//p")[[1]]) #only the first paragraph
#error = function(e){NA}
stats[["topic"]] <- i
stats<- gsub('\\[.*?\\]', '', stats)
#stats<-stats[!duplicated(stats),]
#out.file <- data.frame(rbind(stats,F[i]))
output<-rbind(stats,i)
}
我認爲你的意思是記下錯誤,然後跳到循環的下一個迭代? –
相關/也許重複後http://stackoverflow.com/questions/8093914 – zx8754
作爲一個側面說明,看看http://stackoverflow.com/questions/14693956/how-can-i-prevent-rbind-from -geting-really-slow-as-dataframe-grow-greater – konvas