2 回答
![?](http://img1.sycdn.imooc.com/533e4d510001c2ad02000200-100-100.jpg)
TA贡献1811条经验 获得超5个赞
在文档的代码片段部分下有一个示例,建议使用包装函数来处理错误处理。
def limit_handled(cursor):
while True:
try:
yield next(cursor)
except tweepy.RateLimitError:
time.sleep(15 * 60)
except StopIteration:
print("Done")
break
for follower in limit_handled(tweepy.Cursor(api.followers, id=root_usr).items()):
print(follower.id)
另外,我建议将 count 设置为最大值,tweepy.Cursor(api.followers, id=root_usr, count=200).items()以充分利用每个 API 调用。
![?](http://img1.sycdn.imooc.com/545865470001bf9402200220-100-100.jpg)
TA贡献2016条经验 获得超9个赞
您可以在变量上构造Cursor实例并next(cursor)在 try-catch 中调用。您可以这样处理 StopIteration 和 TweepError。
喜欢
cursor = tweepy.Cursor(api.followers, id=root_usr).items()
# or tweepy.Cursor(api.followers, id=root_usr).items().iter()
while True:
try:
follower = next(cursor)
print(follower.id)
except StopIteration:
break
except tweepy.TweepError:
# Handle this, sleep, log, etc.
pass
添加回答
举报