【问题标题】:python - Traceback (most recent call last): ...python - Traceback(最近一次调用最后一次):...
【发布时间】:2016-10-23 20:58:07
【问题描述】:

我尝试使用 Python 和 tweepy 从 Twitter 收集数据。

我的代码是:

import tweepy

consumer_key="..."
consumer_secret="..."
access_key = "..."
access_secret = "..."


class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print (status.text)

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['capital'], async=True) 

在我的控制台中,python 返回:

RT @gucamo74: @rubenuria eso es muy difícil. El Sevilla no tiene el halo protector arbitral de los equipos de la capital y del Barcelona.
RT @TonySantanaZA: @woznyjs On Macro scale, we failed 2 create Corporate stability, 4 investing Companies. Hence Capital flight,2 other mor… "Pour ne pas se faire rouler"...... #Capital

所以没关系,但在几条推文之后,他向我展示了这条消息:

Exception in thread Thread-10:
Traceback (most recent call last):
  File "//anaconda/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "//anaconda/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "//anaconda/lib/python3.5/site-packages/tweepy/streaming.py", line 286, in _run
    raise
RuntimeError: No active exception to reraise

你知道为什么吗?在我问他之前,我希望蒸汽不会停止。

【问题讨论】:

  • +1 表示 stackoverflow 上最通用的主题行! ...但最好将其更改为您的问题的快速摘要。
  • 我认为您有安装问题。这是一个 python 2.x 脚本,但 pythnon3.5 tweepy 正在运行。
  • 谢谢@tdelaney,我已经安装了 Python 3。你认为我需要安装 python 2 吗?
  • print >> sys.stderr, 'Encountered error with status code:', status_code 是一个 python 2.x 样式的打印语句,它将在 python 3 上引发错误。如果将其更改为 print('Encountered error with status code:', status_code, file=sys.stderr) - 与其他打印语句类似 - 您可以避免有问题的 tweepy 错误处理程序。

标签: python twitter anaconda tweepy


【解决方案1】:

我在这篇文章中找到了解决方案:

Unraised exception using Tweepy and MySQL

通过在https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py 检查 tweepy/streaming.py,似乎 tweepy 在处理异常的方式上存在错误,特别是

if exception:
        # call a handler first so that the exception can be logged.
        self.listener.on_exception(exception)
        raise

这个raise应该是raise exception

这很神奇,但它有效......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多