2015-06-14 4 views
1

У меня проблемы с моей программой, только закрывающиеся на случайных этапах, и я не уверен, почему.Внутренняя ошибка Python Обработка

Сначала я подумал, что это произошло потому, что он получал сообщение об ошибке, но добавил ошибку. по какой-то причине он просто закрывается, скажем, несколько дней работы, и ошибка не отображается. код ниже

import requests 
import lxml.html as lh 
import sys 
import time 
from clint.textui import puts, colored 

API_URL = "http://urgmsg.net/livenosaas/ajax/update.php" 

class Scraper (object): 
    id_stamp = 0 

    def __init__(self, timeout, recent_messages=True): 
     self.timeout = timeout 
     self.handlers = [] 
     self.recent_messages = recent_messages 

    def register_handler(self, handler): 
     self.handlers.append(handler) 
     return handler 

    def scrape(self): 
     try: 
      resp = requests.get(API_URL, params={'f': self.id_stamp}).json() 
     except requests.exceptions.ConnectionError as e: 
      puts("Error encountered when connecting to urgmsg: ", newline=False) 
      puts(colored.red(e.__class__.__name__), newline=False) 
      puts(" " + e.message) 
      return 

     if not resp['updated']: 
      return 

     old_id_stamp = self.id_stamp 
     self.id_stamp = resp['IDstamp'] 
     # if old_id_stamp is 0, this is the first scrape 
     # which will return a whole bunch of recent past messages 
     if not self.recent_messages and old_id_stamp == 0: return 

     # Pager messages are returned newest to oldest, we want to 
     # process them oldest to newest 
     frags = lh.fragments_fromstring(resp['data'])[::-1] 
     for frag in frags: 
      msg = PagerMessage(frag) 
      for handler in self.handlers: 
       handler(msg) 

    def run(self): 
     while True: 
      self.scrape() 
      time.sleep(self.timeout) 

class PagerMessage: 
    def __init__(self, fragment): 
     children = fragment.getchildren() 
     self.datetime = children[0].text 
     self.text = children[1].text 
     # channel starts with `- ` 
     self.channel = children[1].getchildren()[0].text[2:] 
     self.response = 'CFSRES' in self.text 
    def __str__(self): 
     return "{} [{}]: {}".format(self.channel, self.datetime, self.text) 

if __name__ == "__main__": 
    scraper = Scraper(5) 
    @scraper.register_handler 
    def handler(msg): 
     puts(colored.yellow(msg.channel), newline=False) 
     puts(" [", newline=False) 
     puts(colored.green(msg.datetime), newline=False) 
     puts("] ", newline=False) 
     if msg.response: 
      puts(colored.red(msg.text)) 
     else: 
      puts(msg.text) 
    scraper.run() 

Я установил эту часть неправильно?

except requests.exceptions.ConnectionError as e: 
       puts("Error encountered when connecting to urgmsg: ", newline=False) 
       puts(colored.red(e.__class__.__name__), newline=False) 
       puts(" " + e.message) 
       return 
+0

Было бы трудно ответить, потому что мы даже не знаем, что происходит. И невозможно воспроизвести проблему. Попытайтесь добавить журнал, используйте общие исключения, и это поможет вам узнать вашу ошибку. – sobolevn

+0

OK прочитает, как сделать регистрацию и как добавить и использовать общий случай исключения? – shaggs

+0

Просто добавьте дополнительные 'except:' без исключения в конце. – sobolevn

ответ

1

Как предложено @sobolevn изменения

except: as e: 
       puts("Error encountered", newline=False) 
       puts(colored.red(e.__class__.__name__), newline=False) 
       puts(" " + e.message) 
       return 
+0

Извините, что формат с мобильного не изменится после дома. – Shaggy89

Смежные вопросы