我尝试用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
你知道为什么吗?我想在我问他之前蒸汽不会停止。
答案 0 :(得分:0)
我在这篇文章中找到了解决方案:
通过检查https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py处的tweepy / streaming.py,似乎在处理异常的方式中存在一个错误,特别是
if exception:
# call a handler first so that the exception can be logged.
self.listener.on_exception(exception)
raise
此raise
应为raise exception
这很神奇,但它有效...