首页 文章

对于循环跳过错误

提问于
浏览
0

我想通过for循环获取谷歌趋势数据 . 然而,一个错误阻碍了我 . 在搜索其他堆栈问题后,我仍然无法使其工作 . 有问题的循环:

a2p = for (i in dfurlnames$names1)
{ 
    x<- paste(i)
    gtrends_function3(x)
}

在我的for循环中,我收到以下错误:

Error : res$status_code == 200 is not TRUE

我使用以下包和功能:

获取新的gtrendsR; devtools :: install_github( 'PMassicotte / gtrendsR')

library(gtrendsR)

gtrends_function3 <- function(x)
{
    trend1 = gtrends(c(x), geo = c(""), time = "2014-01-05 2014-10-04")
    trend_df1 = ldply(trend1)
    return(as.numeric(trend_df1$hits))        
}

列表:

dfurlnames$names1 = Ang babaeng humayo, The Bad Batch, Une vie, La La Land,               
The Light Between Oceans, El ciudadano ilustre, Spira Mirabilis, La región 
salvaje, Nocturnal Animals

1 回答

  • 1

    状态代码200指的是HTTP协议,表明一切正常 . 也许,你在for循环中请求的东西太快了 . 添加一个睡眠命令,例如:

    Sys.sleep(1)

    在你的for循环中减慢速度 . 或者,使用tryCatch绕过:

    a2p = for (i in dfurlnames$names1)
    { 
        tryCatch({
           x<- paste(i)
           gtrends_function3(x)
        }, error=function(e) {print(e)})
    }
    

相关问题