【问题标题】:Using Tweepy to turn a list of screen names into user IDs使用 Tweepy 将屏幕名称列表转换为用户 ID
【发布时间】:2018-04-19 00:13:13
【问题描述】:

我正在使用 Python 和 Tweepy 编写一个基本程序,以获取 Twitter 屏幕名称列表并下拉相应的用户 ID。我已经实现了速率限制器并且程序可以工作,但是当它遇到我的异常处理时事情就崩溃了。它告诉我 X 中的屏幕名称在等待 15 分钟后不存在。我需要异常处理,因为 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()

【问题讨论】:

    标签: python loops twitter exception-handling tweepy


    【解决方案1】:

    我猜 TweepError 涵盖了多种错误,包括速率限制错误和查询错误。如果您正在搜索不再存在的用户名,您可能会遇到同样的错误。

    查看如何在此处打印您遇到的确切错误类型: Get the error code from tweepy exception instance

    我会在你的 except tweepy.TweepError 捕获中添加一个 if-else 语句,以检查错误是速率限制错误还是链接中解释的其他内容。在后一种情况下,您只需传递(或打印错误和您所做的特定查询)。

    【讨论】:

    • 我的 api 实现使用“等待速率限制器 = True”来解释速率限制器。它在我的其他 Tweepy 程序中运行良好。不过,我会考虑打印出错误,也许还有 if else。
    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 2018-03-26
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2015-05-27
    • 2017-08-14
    相关资源
    最近更新 更多