使用循环 twitteR 查询处理 404 和其他不良响应

编程入门 行业动态 更新时间:2024-10-24 07:22:03
本文介绍了使用循环 twitteR 查询处理 404 和其他不良响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如果我的循环在出现错误时不会中断,我会很高兴.我希望它继续进行下一次迭代.这个例子是我得到的错误和循环中断的最小例子.在我的实际应用程序中,我正在遍历我从另一个脚本生成的一些关注者.

图书馆(twitteR)#设置oauth...for(i 在 1:10) {+ x <- getUser("nalegezx") }

<块引用>

twInterfaceObj$doAPICall(paste("users", "show", sep = "/"), params = params, 中的错误:客户端错误:(404) 未找到

我知道这个循环只会重写对 x 的相同响应.我只是对不打破循环感兴趣.

解决方案

我不是 R Twitter API 的专家,但我可以建议您考虑调用 getUser()在像这样的 try 块中:

for (i in 1:10) {x <- try(getUser("sdlfkja"))}

这应该会阻止您的代码在循环中间崩溃.如果你想在循环中出现警告或错误时也有单独的逻辑,你可以使用 tryCatch:

for (i in 1:10) {x <- tryCatch(getUser("sdlfkja"),警告 = 函数(w){打印(警告");# 在这里处理警告},错误 = 函数(e){打印(错误");# 在这里处理错误})}

I would like it if my loop wouldn't break if I get an error. I want it to just move on to the next iteration. This example is a minimal example of the error I'm getting and the loop breaking. In my real application I'm iterating through some of the followers I've generated from another script.

library(twitteR)
#set oauth...
for(i in 1:10) {
+ x <- getUser("nalegezx") }

Error in twInterfaceObj$doAPICall(paste("users", "show", sep = "/"), params = params, : client error: (404) Not Found

I understand that this loop would simply rewrite the same response to x. I'm just interested in not breaking the loop.

解决方案

I'm not an expert in the R Twitter API, but I can suggest that you consider placing your call to getUser() inside a try block like this:

for (i in 1:10) {
    x <- try(getUser("sdlfkja"))
}

This should stop your code from crashing in the middle of the loop. If you want to also have separate logic when a warning or error occurs in the loop, you can use tryCatch:

for (i in 1:10) {
    x <- tryCatch(getUser("sdlfkja"),
                  warning = function(w) {
                                print("warning"); 
                                # handle warning here
                            },
                  error = function(e) {
                              print("error");
                              # handle error here
                          })
}

这篇关于使用循环 twitteR 查询处理 404 和其他不良响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

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

发布评论

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

>www.elefans.com

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