更改追溯(最新通话最后)显示“错误:网站不可用”
我发现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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的方法是使用
try-except
。语法非常容易:
如果您可能需要处理不同的异常,则可以将特定的异常和错误添加到除外。如果离开空白,您将处理每个错误。
如果您必须在例外加薪后要关闭连接,则可以添加
最后
。The best way to do this is using a
try-except
.The syntax is pretty easy:
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: