Search code examples
rloopsweb-scrapingerror-handlingrvest

R Rvest for() and Error server error: (503) Service Unavailable


I'm new to webscraping, but I am excited using rvest in R. I tried to use it to scrape particular data of companies. I have created a for loop (171 urls), and when I am running it stops on 6th or 7th url with an error

Error in parse.response(r, parser, encoding = encoding) : 
  server error: (503) Service Unavailable

When I start my loop from 7th url it goes for two or three more and stops again with the same error. My loop

library(rvest)    
thing<-c("http://www.informazione-aziende.it/Azienda_ LA-VIS-S-C-A",                                                                                  
    "http://www.informazione-aziende.it/Azienda_ L-ANGOLO-DEL-DOLCE-DI-OBEROSLER-MARCO",                                                         
    "http://www.informazione-aziende.it/Azienda_ MARCHI-LAURA",                                                                                 
    "http://www.informazione-aziende.it/Azienda_ LAVIS-PIZZA-DI-GASPARETTO-MATTEO",                                                              
    "http://www.informazione-aziende.it/Azienda_ LE-DELIZIE-MOCHENE-DI-OSLER-NICOLA",                                                            
    "http://www.informazione-aziende.it/Azienda_ LE-DELIZIE-S-N-C-DI-GAMBONI-PIETRO-E-PISONI-MAURO-C-IN-SIGLA-LE-DELIZIE-S-N-C",                 
    "http://www.informazione-aziende.it/Azienda_ LE-FONTI-DISTILLATI-DI-COVI-MARCELLO",                                                          
    "http://www.informazione-aziende.it/Azienda_ LE-MIGOLE-DI-MATTEOTTI-LUCA",                                                                   
    "http://www.informazione-aziende.it/Azienda_ LECHTHALER-DI-TOGN-LUIGI-E-C-S-N-C",                                                            
    "http://www.informazione-aziende.it/Azienda_ LETRARI-AZ-AGRICOLA")

    thing<-gsub(" ", "", thing)

    d <- matrix(nrow=10, ncol=4)
    colnames(d)<-c("RAGIONE SOCIALE",'ATTIVITA', 'INDIRIZZO', 'CAP')

    for(i in 1:10) {
            a<-thing[i]

            urls<-html(a)

            d[i,2] <- try({ urls %>% html_node(".span") %>% html_text() }, silent=TRUE)
    }

May be there is a way to avoid this error, thank you in advance, any help would be appreciated.

UPD With next code, I am trying to restart the loop of fetching data, from the last successful one with repeat(), but it is looping infinitely, hope for some suggestions.

    for(i in 1:10) {

  a<-thing[i]

  try({d[i,2]<- try({html(a) }, silent=TRUE)  %>%
         html_node(".span") %>%
         html_text() }, silent=TRUE)

  repeat {try({d[i,2]<- try({html(a) }, silent=TRUE)  %>%
                 html_node(".span") %>%
                 html_text() }, silent=TRUE)}
  if (!is.na(d[i,2])) break
}

Or with while()

for(i in 1:10) {

  a<-thing[i]

while (is.na(d[i,2])) {
  try({d[i,2]<-try({html(a) %>%html_node(".span")},silent=TRUE) %>% html_text() },silent=TRUE)
}
}

While() works but not so good and too slow ((


Solution

  • It looks like if you hit that site too quickly, you get a 503. Add a Sys.sleep(2) and all 10 iterations worked for me...

    library(rvest)    
    thing<-c("http://www.informazione-aziende.it/Azienda_ LA-VIS-S-C-A",                                                                                  
             "http://www.informazione-aziende.it/Azienda_ L-ANGOLO-DEL-DOLCE-DI-OBEROSLER-MARCO",                                                         
             "http://www.informazione-aziende.it/Azienda_ MARCHI-LAURA",                                                                                 
             "http://www.informazione-aziende.it/Azienda_ LAVIS-PIZZA-DI-GASPARETTO-MATTEO",                                                              
             "http://www.informazione-aziende.it/Azienda_ LE-DELIZIE-MOCHENE-DI-OSLER-NICOLA",                                                            
             "http://www.informazione-aziende.it/Azienda_ LE-DELIZIE-S-N-C-DI-GAMBONI-PIETRO-E-PISONI-MAURO-C-IN-SIGLA-LE-DELIZIE-S-N-C",                 
             "http://www.informazione-aziende.it/Azienda_ LE-FONTI-DISTILLATI-DI-COVI-MARCELLO",                                                          
             "http://www.informazione-aziende.it/Azienda_ LE-MIGOLE-DI-MATTEOTTI-LUCA",                                                                   
             "http://www.informazione-aziende.it/Azienda_ LECHTHALER-DI-TOGN-LUIGI-E-C-S-N-C",                                                            
             "http://www.informazione-aziende.it/Azienda_ LETRARI-AZ-AGRICOLA")
    
    thing<-gsub(" ", "", thing)
    
    d <- matrix(nrow=10, ncol=4)
    colnames(d)<-c("RAGIONE SOCIALE",'ATTIVITA', 'INDIRIZZO', 'CAP')
    
    for(i in 1:10) {
      print(i)
      a<-thing[i]  
      urls<-html(a)  
      d[i,2] <- try({ urls %>% html_node(".span") %>% html_text() }, silent=TRUE)
      Sys.sleep(2)
    }