【问题标题】:How to get the contents of the error page in Python's mechanize?如何在 Python 的 mechanize 中获取错误页面的内容?
【发布时间】:2013-09-16 22:09:11
【问题描述】:

所以假设我在使用mechanize.Browser.retrieve 时收到了一个错误,并且我像这样捕获它:

try:
  br.retrieve(url, fname)
except mechanize.HTTPError as e:
  if e.code in [403, 404]:
    # how can I get to the contents of the server-sent error page?
  else:
    raise

此时如何访问服务器发送的错误页面?

我尝试使用 br.response().get_data(),但使用 retrieve() 时似乎没有填充。

【问题讨论】:

    标签: python-2.7 mechanize


    【解决方案1】:

    由于 HTTP 错误由 mechanize 包装并包含有关响应的其他信息,因此您可以使用 e.read():

    try:
        br.retrieve(url, fname)
    except mechanize.HTTPError as e:
        if e.code in [403, 404]:
            print e.read()
        else:
            raise
    

    【讨论】:

      猜你喜欢
      • 2019-04-07
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多