running platform: Windows
python version: python3.x
Ide:sublime Text3
reprint please indicate author and source:http://blog.csdn.net/c406495762/article/details/59488464
I. Urllib.error
Urllib.error can receive urllib.request-generated exceptions. Urllib.error has two methods, Urlerror and Httperror. As shown in the following:
Urlerror is a subclass of OSError, Httperror is a subclass of Urlerror, the response of HTTP on the server will return a status code, according to this HTTP status code, we can know whether our visit is successful. For example, the 200 status code mentioned in the second note indicates a successful request, such as a common 404 error.
1.URLError
Let's first look at the urlerror exception, create the file urllib_test06.py, write the following code:
# -*- coding: UTF-8 -*-from urllib import requestfrom urllib import errorif __name__ == "__main__": #一个不存在的连接 url = "http://www.iloveyou.com/" req = request.Request(url) try: response = request.urlopen(req) html = response.read().decode(‘utf-8‘) print(html) except error.URLError as e: print(e.reason)
We can see the following running results:
2.HTTPError
Then look at the httperror exception, create the file urllib_test07.py, write the following code:
# -*- coding: UTF-8 -*-from urllib import requestfrom urllib import errorif __name__ == "__main__": #一个不存在的连接 url = "http://www.douyu.com/Jack_Cui.html" req = request.Request(url) try: responese = request.urlopen(req) # html = responese.read() except error.HTTPError as e: print(e.code)
After running, we can see 404, this indicates that the requested resource is not found on the server, www.douyu.com this server is present, but we are looking for jack_cui.html resources is not, so throw 404 exception.
Two. Mixed use of Urlerror and Httperror
Finally, it is worth noting that if you want to catch an exception with Httperror and Urlerror, you need to put httperror in front of Urlerror, because Httperror is a subclass of Urlerror. If Urlerror is placed in front, an HTTP exception will respond to Urlerror first, so that the httperror will not catch the error message.
If the above method is not used, you can also use the HASATTR function to determine the properties Urlerror contains, if the containing reason attribute indicates urlerror, if the containing code attribute indicates httperror. To create the file urllib_test08.py, write the following code:
#-*-coding:utf-8-*-from urllib Import Requestfrom urllib import error if __name__ = "__main__": # A non-existent connection URL = "http://www.douyu.com/Jack_Cui.html" req = Request. Request (URL) try:responese = Request.urlopen (req) except Error. Urlerror as e: if hasattr (E, ' code ' ) Print ( "Httperror") print (E.code) elif hasattr (E, ' reason ') print ( "Urlerror") print (E.reason)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
The results of the operation are as follows:
Python3 Network Crawler (iii): Urllib.error exception