【发布时间】: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