几小时后,Tweepy停下来了



我使用tweepy来传输一些tweet。这是我的程序:

import tweepy
import json
consumer_key = "***" 
consumer_secret = "***"
access_token_key="***"
access_token_secret="***"
auth1 = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth1.set_access_token(access_token_key, access_token_secret)
api = tweepy.API(auth1)
class StreamListener(tweepy.StreamListener):
    def on_status(self, status):
        try:
            print status.text
        except Exception, e:
            print 'Encountered Exception Tweet:', e
            pass
        return True
    def on_error(self, status_code):
        print 'Encountered error with status code:' + repr(status_code)
        return True 
    def on_data(self, data):
        if 'in_reply_to_status_id' in data:
            status = tweepy.Status.parse(self.api, json.loads(data))
            if self.on_status(status) is False:
                return True
        elif 'delete' in data:
            delete = json.loads(data)['delete']['status']
            if self.on_delete(delete['id'], delete['user_id']) is False:
                return True
        elif 'limit' in data:
            if self.on_limit(json.loads(data)['limit']['track']) is False:
                return True
        return True
    def on_timeout(self):
        print 'Timeout...'
        return True
l = StreamListener()
streamer = tweepy.Stream(auth=auth1, listener=l, timeout=36000000)
setTerms = ['enbrel']
streamer.filter(follow=None,track = setTerms)

2/3小时后,此过程停止。无信号错误、超时等。它只是没有得到更多的推特。我哪里做错了?

尝试在类中添加一个on_disconnect方法。这可能是Twitter正在断开您的连接(不是错误,也不是超时),而您没有处理此问题。如果你愿意,你可以用不同的方式处理不同的Twitter错误。

def on_disconnect(self, notice):
    """Called when twitter sends a disconnect notice
    Disconnect codes are listed here:
    https://dev.twitter.com/docs/streaming-apis/messages#Disconnect_messages_disconnect
    """
    return

查看tweepy的streaming模块以获取更多信息。

你也可以尝试在你的streamer.filter()中启用失速警告。下面是Tweepy源文件中的所有选项及其默认值:

def filter(self, follow=None, track=None, async=False, locations=None,
           stall_warnings=False, languages=None, encoding='utf8'):

您可能希望初始化api时设置超时以

开始
api = tweepy.API(auth1,timeout=60)

相关内容

  • 没有找到相关文章

最新更新