gpt4 book ai didi

python - 在 tweepy 中捕获所有追随者

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:01 24 4
gpt4 key购买 nike

我想在 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)

当我运行这部分时,它会 try catch 以下内容。在我的线程中捕获的用户数是 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

关于python - 在 tweepy 中捕获所有追随者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com