跳过错误?

时间:2014-04-18 19:20:25

标签: python twitter tweepy

嗨,我想知道是否有人可以提供帮助:

我在下面的代码与Twitter的API联系,以便从Twitter ID号获得转推计数。我想知道是否有人能指出我正确的方向如何输入代码,以便它跳过模块(Tweepy)不识别的ID号而不是完全停止?

编辑:错误是" TweepError:[{u'消息':你'抱歉,该页面不存在',你'代码':34 }]"

谢谢!

import tweepy


consumer_key=""
consumer_secret=""
access_key = ""
access_secret = "" 


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

id_list = ["4000000000000",
"etc.....",
]

def get_retweet_count(tweet_id):
    tweet = api.get_status(tweet_id)
    return tweet.retweet_count

for id in id_list:
    print get_retweet_count(id), id

2 个答案:

答案 0 :(得分:4)

您想使用exception handling - 即在任何抛出您认为可以处理的异常的代码周围放置一个try except子句。

例如

try:
    print get_retweet_count(id)
except TweepError, e:
    print e
    print "carrying on anyway"

答案 1 :(得分:1)

正如编码器所提到的,try-except块应该可以解决问题:

for id in id_list:
    try:
        print get_retweet_count(id), id
    except:
        pass