-1

我正在使用 twython 从 twitter 获取一些推文。要获取我使用 try / except 的推文,例如

for follower in followers_file_id:
    follower = follower.strip()
    try:     
      if req_user_settings > max_user_settings_req or req_user_tweets > max_tweets_req:
        time.sleep(15 * 60)
        tweets_file_name = tweetsFileName(followed_user)

      user_settings = twitter.show_user(user_id = follower)
      req_user_settings += 1

      if not user_settings['protected'] and user_settings['statuses_count'] > 0:
        tweets = twitter.get_user_timeline(user_id = follower, count = 200, page = 1)
        req_user_tweets += 1

      n_tweets =tweets[0]['user']['statuses_count']
      n_loops = n_tweets // max_tweets_per_page + 1
      if n_loops > max_tweet_pages:
        n_loops = max_tweet_pages

      if tweets[0]['user']['lang'] == 'en':
        n_crawled_tweets = writeTweets(tweets, tweets_file_name, n_crawled_tweets)

      for i_loop in range(1, n_loops):        
        tweets = twitter.get_user_timeline(user_id = follower, count = 200, page = i_loop+1)
        n_crawled_tweets = writeTweets(tweets, tweets_file_name, n_crawled_tweets)
        req_user_tweets += 1

    except TwythonError as e:
      print str(e)

打印错误后如何告诉程序继续执行?原因现在打印错误后,程序停止。

4

2 回答 2

1

一旦发生异常,整个 try 块将被留下。如果您想在异常后继续,您需要更早地捕获异常

喜欢

Try:
   some code here
except TwythonError as e:
   print e

some more code here
于 2013-06-18T09:04:59.553 回答
1
try:
  some code here
except TwythonError as e:
  print e
finally:
   #continue

请记住,代码将始终通过 finally 部分,因此,您只在 try 部分执行引发异常的代码,其余部分在 finally 部分执行。

于 2013-06-18T09:05:41.537 回答