【问题标题】:Weird Exception in python code - mechanize and beautifulsouppython代码中的奇怪异常-mechanize和beautifulsoup
【发布时间】:2016-12-14 12:06:26
【问题描述】:

我的应用程序中出现了那个奇怪的错误。 这不是由于机械化库而生成的,因为我已经为类 Exception 在它周围放置了一个 try-except。

加上 browser.open() 返回所需的地址,没有任何问题。 此外,这个例外不会出现 10 次。

我不知道它是什么......

请帮忙

这是代码:

def check_result(self, submission_id, question_code):
    """
    returns the result of a problem submission.
    :return: result codde
    RA - right answer
    WA - wrong answer
    CE - Compilation error
    RE - Runtime Error
    """
    print "================================"
    print "Response:"
    try:
        response = self._br.open(self.URL + '/status/' + question_code)
    except Exception: # TODO get more specific exception for better stack trace
        raise ExceptionSet.InternetConnectionFailedException
    # print response.read()
    response = BeautifulSoup(response.read(), 'html.parser')
    tables = response.findChildren('table')
    table = tables[0]
    rows = table.findChildren(['tr', 'th'])
    result = ''
    flag = False
    for row in rows:
        cells = row.findChildren('td')
        for cell in cells:
            if cell.string == submission_id:
                flag = True
                result = cell.string
                break
        if flag:
            break
    print result

确切的堆栈跟踪是:

Exception mechanize._response.httperror_seek_wrapper: 
<httperror_seek_wrapper (urllib2.HTTPError instance) at 0x7fd87195fbb0 
whose wrapped object = <closeable_response at 0x7fd87490dc20 whose fp = 
<response_seek_wrapper at 0x7fd87195c830 whose wrapped object = 
<closeable_response at 0x7fd87195c2d8 whose fp = <socket._fileobject 
object at 0x7fd8719706d0>>>>> in <bound method API.__del__ of 
<CodeChef.API instance at 0x7fd8749249e0>> ignored

谢谢!

如果这是一个明显而直接的问题,我们深表歉意。

编辑: 显然问题不在于这段代码, 我仍在努力解决它。如果您能提供任何真正有用的线索。

代码的链接是: https://github.com/ParadoxZero/CodechefAPI

如果我应该直接在此处发布代码,请发表评论。

谢谢!

编辑2:

找到错误的原因,但仍然不明白为什么会发生。我在 __ del __() 中调用 logout() 函数 删除 __ del __() 纠正了错误。

但仍然不明白它是什么或为什么会发生。

【问题讨论】:

    标签: python-2.7 beautifulsoup mechanize stack-trace


    【解决方案1】:

    找到了解决方案,该错误的发生是由于析构函数中发生了一个未处理的简单异常。

    只需编写代码来处理异常或重新抛出它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      相关资源
      最近更新 更多