且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R(R)for()和错误服务器错误:(503)服务不可用

更新时间:2022-05-14 01:12:20

看起来像是你打得太快了,你得到一个503。添加一个 Sys.sleep(2),并为我的所有10次迭代...

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)
}