在 tweepy 中捕获所有追随者

Capturing all followers in tweepy

我想打印所有关注者或 Twitter 关注者:

while True:
    try:
       for user in tweepy.Cursor(api.followers,screen_name='TestUser').items():
            print user.screen_name
       break
    except tweepy.TweepError:
        time.sleep(60*20)

当我 运行 这部分时,它会尝试捕获以下内容。在我的线程中捕获的用户数是 200。但它在 20 分钟睡眠后不会继续...它会尝试但会再次捕获用户。

我该如何解决?

每次在 for user in... 行中创建新的迭代器时,每次 while 循环迭代都会重新开始。

尝试使用发电机:

def handle_errors(cursor):
    while True:
        try:
            yield cursor.next()
        except tweepy.TweepError:
            time.sleep(20 * 60)

for user in handle_errors(tweepy.Cursor(api.followers,screen_name='TestUser').items()):
     print user.screen_name