使用api.retweet()Tweepy 3.5/Python3时处理错误时出错

使用api.retweet()Tweepy 3.5/Python3时处理错误时出错,python,twitter,tweepy,Python,Twitter,Tweepy,我的应用程序是使用Tweepy转发标签。转发标签的工作,我有麻烦让错误2和3的工作。 错误 1.您自己的id(完成) 2.如果tweet已经被删除 3.如果发送到RT的tweet来自受保护的源 在StdOutListener中访问api.retweet(doTweet)不允许它出现on_error()。我还能怎么做?我是巨蟒 class StdOutListener(tweepy.StreamListener): def on_data(self, data):

我的应用程序是使用Tweepy转发标签。转发标签的工作,我有麻烦让错误2和3的工作。 错误 1.您自己的id(完成) 2.如果tweet已经被删除 3.如果发送到RT的tweet来自受保护的源

在StdOutListener中访问api.retweet(doTweet)不允许它出现on_error()。我还能怎么做?我是巨蟒

class StdOutListener(tweepy.StreamListener):
    def on_data(self, data):      
        all_data = json.loads(data)
        username = all_data["user"]["screen_name"]
        doTweet = all_data["id"]
        if username != our_own_id:
            #make sure you haven't already retweeted
            #make sure tweets aren't protected
            print(username) # just so we know it's working
            api.retweet(doTweet)
        return True

    def on_error(self, status_code):
        print('error')
        read_error = json.loads(status_code)
        print('Got an error with status code: ' + str(read_error))          
        return True # To continue listening

    def on_timeout(self):
        print('Timeout...')
        return True # To continue listening


您可以在数据方法中修改对
api.retweet
的调用,如下所示:

...
if username != our_own_id:
    print(username) # just so we know it's working
    try:
        api.retweet(doTweet)
    except tweepy.TweepError as e:
        # add here a more complex error handling
        print(e)

希望能有所帮助。

您可以在
on_data
方法中修改对
api.retweet
的调用,如下所示:

...
if username != our_own_id:
    print(username) # just so we know it's working
    try:
        api.retweet(doTweet)
    except tweepy.TweepError as e:
        # add here a more complex error handling
        print(e)

希望有帮助。

我尝试将api.retweet()放在main中,但没有成功。我不明白错误是什么。请把你的头发修好indentation@ValentinLorentz我应该在缩进中更改什么?这就是我从空闲状态复制粘贴的内容。@ValentinLorentz刚刚重新复制粘贴,try:行现在格式怪异。所以这就是一个问题:如何格式化?错误:添加了stream.filter(track=['#love'])(#love收到了很多tweet)。我正在寻找一种方法来检查此错误消息:tweepy.error.TweepError:[{'message':'您已经转发了此推文','code':327}]我尝试将api.retweet()放入main中,但没有成功。我不明白错误是什么。请把你的头发修好indentation@ValentinLorentz我应该在缩进中更改什么?这就是我从空闲状态复制粘贴的内容。@ValentinLorentz刚刚重新复制粘贴,try:行现在格式怪异。所以这就是一个问题:如何格式化?错误:添加了stream.filter(track=['#love'])(#love收到了很多tweet)。我正在寻找一种方法来检查此错误消息的错误:tweepy.error.tweeperor:[{'message':'您已经转发了此tweet','code':327}]