更改追溯(最新通话最后)显示“错误:网站不可用”

发布于 2025-02-10 20:38:55 字数 847 浏览 1 评论 0原文

我发现Python是一种爱好,所以现在我正在尝试解决问题。

当网站不响应(HTTP错误503:不可用的服务)时,我会知道

Traceback (most recent call last):
  File "c:\Users\dawid\Studia\Programowanie\HTML.py", line 30, in <module>
    start()
  File "c:\Users\dawid\Studia\Programowanie\HTML.py", line 21, in start
    response = browser.open(oceny)
  File "C:\Users\dawid\Studia\Programowanie\.venv\lib\site-packages\mechanize\_mechanize.py", line 257, in open
    return self._mech_open(url_or_request, data, timeout=timeout)
  File "C:\Users\dawid\Studia\Programowanie\.venv\lib\site-packages\mechanize\_mechanize.py", line 313, in _mech_open
    raise response
mechanize._response.get_seek_wrapper_class.<locals>.httperror_seek_wrapper: HTTP Error 503: Service Unavailable

如何更改它以显示诸如“有HTTP错误:503-等待几分钟”之类的内容?

I found Python as a hobby so now I'm trying to fix a problem.

When a website is not responding (HTTP Error 503: Service Unavailable), I get

Traceback (most recent call last):
  File "c:\Users\dawid\Studia\Programowanie\HTML.py", line 30, in <module>
    start()
  File "c:\Users\dawid\Studia\Programowanie\HTML.py", line 21, in start
    response = browser.open(oceny)
  File "C:\Users\dawid\Studia\Programowanie\.venv\lib\site-packages\mechanize\_mechanize.py", line 257, in open
    return self._mech_open(url_or_request, data, timeout=timeout)
  File "C:\Users\dawid\Studia\Programowanie\.venv\lib\site-packages\mechanize\_mechanize.py", line 313, in _mech_open
    raise response
mechanize._response.get_seek_wrapper_class.<locals>.httperror_seek_wrapper: HTTP Error 503: Service Unavailable

How can I change it to display something like "There is a HTTP Error: 503 - wait a few minutes"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏已如深秋| 2025-02-17 20:38:55

最好的方法是使用try-except

语法非常容易:

try:
    # code that throws exception here
except:
    print("There is a HTTP Error: 503 - wait a few minutes")

如果您可能需要处理不同的异常,则可以将特定的异常和错误添加到除外。如果离开空白,您将处理每个错误。
如果您必须在例外加薪后要关闭连接,则可以添加最后

try:
    # code that throws exception here
except:
    print("There is a HTTP Error: 503 - wait a few minutes")
finally:
    # code that closes everything safely

The best way to do this is using a try-except.

The syntax is pretty easy:

try:
    # code that throws exception here
except:
    print("There is a HTTP Error: 503 - wait a few minutes")

You can add specific Exceptions and Errors into the except clause, if you maybe have to handle different exceptions. If left blank, you will handle every error.
In case, you can also add a finally if you have to close a connection after the exception raise,, like so:

try:
    # code that throws exception here
except:
    print("There is a HTTP Error: 503 - wait a few minutes")
finally:
    # code that closes everything safely
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文