簡體   English   中英

如何在 R 出錯后繼續 for 循環?

[英]How can you continue the for loop in R even after an error?

我正在解析來自多個鏈接的數據。 但是其中一些鏈接在一段時間后就斷開了。 當我使用rvest package 解析時,它顯示錯誤或警告。 我該怎么做才能繼續用 for-loop 解析,所以它會移動到下一行。


house_link <- "https://somon.tj/adv/7985721_2-komn-dom-grandzavod/"
house_features = data.frame()

for(x in 1:length(house_link)) {
  
   tryCatch({
      page_data = read_html(house_link[x])
      message("Executed.")
  }, error = function(e){
      message('Caught an error!')
      print(e)
  }, warning = function(w){
      message('Caught an warning!')
      print(w)
  }, finally = {
      message('All done, quitting.')
  }
)    
    pricing = page_data %>% html_nodes(".css-13sm4s4") %>% 
      html_element("span") %>% html_text() 
    house_features = rbind(house_features, data.frame(pricing, stringsAsFactors = FALSE))
}

也許是這樣的?

library(rvest)

house_link <- "https://lalafo.kg/bishkek/ads/104-seria-2-komnaty-47-kv-m-s-mebelu-kondicioner-zivotnye-ne-prozivali-id-95221626"
house_features = data.frame()

for(x in 1:3) { # seq_along(house_link)  <- if you have more than 1 link this is the correct method
  
  cat('Link', x)
  
  start_time <- Sys.time()
  if (x %% 200 == 0) {
    Sys.sleep(5)
    print("pausing ...")}
  
  page_data <- tryCatch({
    page_data = read_html(house_link[x])
    message("Executed.")
  }, error = function(e){
    message('\nCaught an error!')
    return(NA) # here a return variable for testing is returned in the error condition - notice that this has to be initiated with the return function
  }, finally = {cat('Continuing with', x+1,'\n')})   #; next()})  <-  disabled next()
  
  ## This part is handled by finally next()
  ############################
  if(is.na(page_data)){      #
    cat('this is a test\n')  #
    next()                   #
    }                        #
  ############################
  
  else{  # else is not strictly necessary but the point may be easier to contextualised like this
    pricing = page_data %>% html_nodes(".css-13sm4s4") %>% 
      html_element("span") %>% html_text() 
    house_features = rbind(house_features, data.frame(pricing, stringsAsFactors = FALSE))
  }
}

Link 1
Caught an error!
Continuing with 2 
this is a test

Link 2
Caught an error!
Continuing with 3 
this is a test

Link 3
Caught an error!
Continuing with 4 
this is a test

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM