On a reptile Prepares 9: Exception Handling

Exception handling ## pages
       an outstanding feature is that it can deal with each leisurely encounters an error, and will not say because encountered a small mistake led to the collapse of the entire program, most of the high-level language processing error method (high-level language by detecting all abnormalities, abnormal processing to achieve, Python is the same.)
       ※ when to use the Internet access code with the program, there will be an exception and that is perfectly normal, and for example, said before the implementation of a broker, through 10 several dozens of agents to achieve ip crawlers visit, if one proxy ip suddenly does not respond, then it will error, such an error trigger rate is very high, you can use all the ip that there is ghost blanket. However, the emergence of a proxy ip can not and will not affect the mission of the whole script, so we catch these exceptions only way to deal with it need to ignore it it.
Here Insert Picture Description
       ※ URLError: When our urlopen () method can not handle a response when an exception is thrown a URLError, often in the absence of a network connection, or other server simply do not exist and they will trigger this exception, at the same time, this abnormalities accompanied URLError give a reason attribute for the tuple by the error code and the error information thereof.
Here Insert Picture Description
       (Analysis: When we tried to access a domain name does not exist, the following error is error number 11001, then an error message is accompanied by getaddrinfo failed, is to get the address wrong information, because they are fundamentally not the domain name.)

       ※ HTTPError:是 URLError 的一个子类,服务器上每一个 http 的响应都会返回一个数字的状态码,例如我们看到的 404,表示资源找不到,有时候状态码会指出服务器无法完成的请求类型,一般情况下,Python 会帮你处理一部分的这类请求,例如说响应一个重定向—>(重定向及其特点) 要求客户端从别的地方来获取文档,那么这个 urllib 模块就会自动帮你处理这个响应,但是,在有一些情况下,它是无法帮你处理的,例如我们刚才说的 404,页面无法找到,它没办法帮你处理,需要你人工来进行过滤。也有 403(请求禁止),401(需要人工验证),想要了解更多最新的状态码,可以去百度搜索HTTP状态码就可以了http://tools.jb51.net/table/http_status_code

       (那么当出现一个错误的时候,服务器就会返回一个 HTTP 错误号和错误的页面,你可以使用 HTTPError 实例对象作为页面返回的响应对象,它同样也拥有 read() 和 geturl() 或者说info() 这类方法)
Here Insert Picture Description
       下面总结:

       处理异常的第一种写法:

from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request(someurl)
try:
    response = urlopen(req)
except HTTPError as e:
    print('The server couldn\'t fulfill the request.')
    print('Error code: ', e.code)
except URLError as e:
    print('We failed to reach a server.')
    print('Reason: ', e.reason)
else:
# everything is fine

       (这一种写法有一点需要注意的就是:except HTTPError 必须写在 except URLError 的前面,这样才会响应到 HTTPError ,因为 HTTPError是 URLError 的子类,如果 except URLError 写在前面,那么 except HTTPError 永远都响应不了。被上面拦截到了)

       处理异常的第二种写法:

from urllib.request import Request, urlopen
from urllib.error import URLError
req = Request(someurl)
try:
    response = urlopen(req)
except URLError as e:
    if hasattr(e, 'reason'):
        print('We failed to reach a server.')
        print('Reason: ', e.reason)
    elif hasattr(e, 'code'):
        print('The server couldn\'t fulfill the request.')
        print('Error code: ', e.code)
else:
# everything is fine

       (Hasattr whether to have this property, look there is no error (including URLError and HTTPError), as long as there is one, it will print reason, then continue to determine whether there is code, if there is code, is HTTPError, then the code also prints out. The second more recommended)

Published 247 original articles · won praise 116 · views 280 000 +

Guess you like

Origin blog.csdn.net/w15977858408/article/details/104130235