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

编程入门 行业动态 更新时间:2024-10-24 01:51:26
本文介绍了R(R)for()和错误服务器错误:(503)服务不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新来的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("www.informazione-aziende.it/Azienda_ LA-VIS-S-C-A", "www.informazione-aziende.it/Azienda_ L-ANGOLO-DEL-DOLCE-DI-OBEROSLER-MARCO", "www.informazione-aziende.it/Azienda_ MARCHI-LAURA", "www.informazione-aziende.it/Azienda_ LAVIS-PIZZA-DI-GASPARETTO-MATTEO", "www.informazione-aziende.it/Azienda_ LE-DELIZIE-MOCHENE-DI-OSLER-NICOLA", "www.informazione-aziende.it/Azienda_ LE-DELIZIE-S-N-C-DI-GAMBONI-PIETRO-E-PISONI-MAURO-C-IN-SIGLA-LE-DELIZIE-S-N-C", "www.informazione-aziende.it/Azienda_ LE-FONTI-DISTILLATI-DI-COVI-MARCELLO", "www.informazione-aziende.it/Azienda_ LE-MIGOLE-DI-MATTEOTTI-LUCA", "www.informazione-aziende.it/Azienda_ LECHTHALER-DI-TOGN-LUIGI-E-C-S-N-C", "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("www.informazione-aziende.it/Azienda_ LA-VIS-S-C-A", "www.informazione-aziende.it/Azienda_ L-ANGOLO-DEL-DOLCE-DI-OBEROSLER-MARCO", "www.informazione-aziende.it/Azienda_ MARCHI-LAURA", "www.informazione-aziende.it/Azienda_ LAVIS-PIZZA-DI-GASPARETTO-MATTEO", "www.informazione-aziende.it/Azienda_ LE-DELIZIE-MOCHENE-DI-OSLER-NICOLA", "www.informazione-aziende.it/Azienda_ LE-DELIZIE-S-N-C-DI-GAMBONI-PIETRO-E-PISONI-MAURO-C-IN-SIGLA-LE-DELIZIE-S-N-C", "www.informazione-aziende.it/Azienda_ LE-FONTI-DISTILLATI-DI-COVI-MARCELLO", "www.informazione-aziende.it/Azienda_ LE-MIGOLE-DI-MATTEOTTI-LUCA", "www.informazione-aziende.it/Azienda_ LECHTHALER-DI-TOGN-LUIGI-E-C-S-N-C", "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)服务不可用

本文发布于:2023-06-13 00:00:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/670297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   不可用   服务器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!