1, Urlerror
Cause of Error:
- Network is not connected, that is, the computer cannot surf the internet
- Cannot connect to a specific server
- Server does not exist
try:except Example:
Import= urllib2. Request ('http://www.xxxxx.com')try: Urllib2.urlopen (Request) except urllib2. Urlerror, E:print E.reason
2, httperrorhttperror instance will have a code property, this is the server sends the related error number. Because URLLIB2 can handle redirects for you, that is, the code that starts with 3 can be processed, and a 100-299 range number indicates success, so you can only see 400-599 of the error number.
req = Urllib2. Request ('http://blog.csdn.net/cqcre')try: urllib2.urlopen (req) except Urllib2. Httperror, E: print e.codeexcept urllib2. Urlerror, E: print e.reasonElse: Print " OK "
The request method is involved in the example, and the use of this method is self-querying.
Python Exception handling