Tweepy似乎随机关闭

时间:2015-07-07 15:21:44

标签: python tweepy

我目前正在尝试用tweepy收集推文。我正在使用tweepy附带的基本示例程序,但是我将其修改为写入文件。有时我的程序运行12个小时,有时一个,但似乎随机关闭,没有错误消息。这是我的代码:

from __future__ import absolute_import, print_function

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream


# Go to http://apps.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key=""
consumer_secret=""

# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token=""
access_token_secret=""
f = open('filename.txt', 'w')
class StdOutListener(StreamListener):
    """ A listener handles tweets are the received from the stream.
    This is a basic listener that just prints received tweets to stdout.

    """
    def on_data(self, data):
        print(data)
        f.write(data)
        return True

    def on_error(self, status):
        print(status)

if __name__ == '__main__':
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    stream = Stream(auth, l)
    stream.filter(track=['keyword'])

感谢。

1 个答案:

答案 0 :(得分:0)

使用try,除了块,您的Stream错误将打印到STDOUT。您可以捕获except块中的错误并重新启动脚本,或记录错误。

try: 
    stream.filter(track=['keyword'])
except Exception, e:
    print "error: %s" % str(e)
    # log error or restart script

我猜测它崩溃的原因是由于类似的常见错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 4: ordinal not in range(128)
相关问题