我试着用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 return:
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你知道为什么吗?我希望在我向他求婚之前,气氛不会停止。
发布于 2016-10-24 05:44:52
我在这篇文章中找到了解决方案:
Unraised exception using Tweepy and MySQL
通过在https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py上检查tweepy/streaming.py,似乎tweepy在处理异常的方式上有一个bug
if exception:
# call a handler first so that the exception can be logged.
self.listener.on_exception(exception)
raise此raise应为raise exception
这是魔术,但它有效..。
https://stackoverflow.com/questions/40207718
复制相似问题