2015-09-11 39 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();