R(R)for()和错误服务器错误:(503)服务不可用 [英] R Rvest for() and Error server error: (503) Service Unavailable
问题描述
我是新来的webscraping,但我很高兴使用 rvest
在R.
我试图用它来刮擦公司的特定数据。
我创建了一个for循环(171 urls),当我运行它停止在第6或第7个url与错误
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
当我从第七个url开始我的循环时,它会再次出现两三次,同样的错误停止。
我的循环
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)
}
一种避免这种错误的方法,谢谢你的意见nce,任何帮助将不胜感激。
May be there is a way to avoid this error, thank you in advance, any help would be appreciated.
UPD
随着下一个代码,我试图重新开始获取数据的循环,从最后一个成功的 repeat()
,但它无限循环,希望有一些建议。
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
}
或 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()
作品但不太好,太慢((
While()
works but not so good and too slow ((
推荐答案
看起来像是你打得太快了,你得到一个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)
}
这篇关于R(R)for()和错误服务器错误:(503)服务不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!