问题描述
我正在尝试抓取一个网站,但不允许我抓取超过9页,是否有什么办法可以让我在9页之后停止循环并中断一两分钟,然后重新开始抓取? >
代码如下:
library(RCurl)
library(stringr)
library(XML)
jt<- c()
for (i in 1:70){
tryCatch({
html<- getURL((url[[i]]),followlocation = TRUE)
doc = htmlParse(html,asText=TRUE)
new <- xpathSApply(doc,"div/a",xmlValue)
jt[[i]] <- new},error=function(e){cat("ERROR :",conditionMessage(e),"\n")})}
解决方法
如果添加if(i %% 9 == 0) {Sys.sleep(60)}
,则每9次迭代将暂停60秒。 %%
运算符返回将i
除以9的余数,因此,如果等于0,则说明您已完成9次迭代。