2015-09-11 35 views
0

这是我的twitterbot脚本。它根据关键字进行搜索并转发包含关键字的推文。我要打破这个循环中循环的和错误代码的情况下重新启动脚本:跳出循环并重新启动python脚本

for row in retweeted_id: 
     try: 
      print(row) 
      twitter.api.retweet(row) 

      time.sleep(180) 
     except tweepy.TweepError, e: 
      if e == "[{u'message': u'You have already retweeted this tweet.', u'code': 327}]": 
       print(e) 
       break      
      elif e == "[{u'message': u'Rate limit exceeded', u'code': 88}]": 
       print(e) 
       time.sleep(60*5) 
      elif e == "[{u'message': u'User is over daily status update limit.', u'code': 185}]": 
       print(e) 
       break 


      else: 
       print(e) 

我试着这样做:

else:` 
    continue 
break 

,我也一直在努力,把整个脚本在一个函数中,但我没有足够的经验来编写函数中的类/函数。 我想在出现错误的情况下在顶部重新启动脚本327 非常感谢您的帮助!

这里是整个脚本:

import time 
retweeted_id = [] 
tweet_text = [] 
tweet_text_id = [] 
from TwitterSearch import TwitterSearchOrder, TwitterUserOrder, TwitterSearchException, TwitterSearch 

try: 
    tso = TwitterSearchOrder() 
    tso.set_keywords([""]) 

    tso.set_language('nl') 
    tso.set_include_entities(False) 
    tso.set_result_type('recent') 

    ts = TwitterSearch(
     consumer_key = "aaaa", 
     consumer_secret = "bbbb", 
     access_token = "cccc", 
     access_token_secret = "dddd" 
         ) 
    for retweeted in ts.search_tweets_iterable(tso): 
     tweet_text_id.append({retweeted['id'], retweeted['user']['screen_name'] }) 
     retweeted_id.append(retweeted['id']) 
    print('done') 

    import tweepy 
    class TwitterAPI: 
     def __init__(self): 
      consumer_key = "aaaa" 
      consumer_secret = "bbbb" 
      auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
      access_token = "cccc" 
      access_token_secret = "dddd" 
      auth.set_access_token(access_token, access_token_secret) 
      self.api = tweepy.API(auth) 

     def tweet(self, message): 
      self.api.update_status(status=message) 

    if __name__ == "__main__": 
     twitter = TwitterAPI() 

     for row in retweeted_id: 
      try: 
       print(row) 
       twitter.api.retweet(row) 

       time.sleep(180) 
      except tweepy.TweepError, e: 
       if e == "[{u'message': u'You have already retweeted this tweet.', u'code': 327}]": 
        print(e) 
        break      
       elif e == "[{u'message': u'Rate limit exceeded', u'code': 88}]": 
        print(e) 
        time.sleep(60*5) 
       elif e == "[{u'message': u'User is over daily status update limit.', u'code': 185}]": 
        print(e) 
        break 

except TwitterSearchException as e: 
    print(e) 
+1

异常通常是突破几个嵌套层次的循环/函数调用/最简单的方法。 – Kevin

回答

1

如果我理解正确,你想要什么,重组这种方式可能是最简单的答案(最好是避免全局变量)。

<imports> 

<globals from init> 

def init(): 
    <your init stuff> 

class TwitterAPI: 
    <...> 

def twit(): 
    twitter = TwitterAPI() 
    for row in retweeted_id: 
     <rest of loop> 

def main(): 
    init(); 
    while (True): 
     try: 
      twit(); 
     except TwitterSearchException as e: 
      print(e) 

if __name__ == "__main__": 
    main();