我正在使用 Python 和 Tweepy 编写一个基本程序,以获取 Twitter 屏幕名称列表并下拉相应的用户 ID。我已经实现了速率限制器并且程序可以工作,但是当它遇到我的异常处理时,事情就会崩溃。它告诉我等待 15 分钟后 X 中的屏幕名称不存在。我需要异常处理,因为 Tweepy 在运行时经常遇到问题。我在这里做错了什么?
f = open('output2.txt', 'w')
while True:
for x in HandleList1:
try:
u = api.get_user(id = x)
print >> f, u.id
except tweepy.TweepError:
print "We just hit an error, waiting for 15min and then reconnecting..."
time.sleep(60*15)
u = api.get_user(id = x)
print >> f, u.id
except StopIteration:
print "Stopping the iteration and processing the results!"
break
f.close()
最佳答案
我猜 TweepError 涵盖了多种错误,包括速率限制错误和查询错误。如果您搜索不再存在的用户名,您可能会收到相同的错误。
查看如何打印您在此处遇到的确切错误类型: Get the error code from tweepy exception instance
我会在 except tweepy.TweepError 捕获中添加一个 if-else 语句,以检查错误是否是速率限制错误或链接中解释的其他错误。在后一种情况下,您可以直接传递(或打印错误和您所做的特定查询)。
关于python - 使用 Tweepy 将屏幕名称列表转换为用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47143605/