Это мой сценарий для 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)
Исключения, как правило, самый простой способ вырваться из нескольких вложенных слоев циклов/вызовов функций/независимо. – Kevin