Why does urllib2.getcode () method drop to 404?

In the Python beginner course, I took Lynda to use .getcode () to get the http code from the URL and one that can be used as a test before reading the data:

webUrl = urllib2.urlopen('http://www.wired.com/tag/magazine-23-05/page/4')
print(str(webUrl.getcode()))
if (webURL.getcode() == 200):
    data = webURL.read()
else:
    print 'error'

However, when used with the 404 page above, Python exits: Python function terminated unexpectedly: HTTP Error 404: Not Foundso it seems like this tutorial was completely wrong?

My question then is what exactly is .getcode () really good? You cannot use it to check what an http code is if you don't know what it is (or at least it's not 404). Was the course wrong or did I miss something?

My understanding is the right way to do this, like this, which does not use .getcode () at all (although tell me if there is a better way):

try:
    url = urllib2.urlopen('http://www.wired.com/tag/magazine-23-05/page/4')
except urllib2.HTTPError, e:
    print e

.getcode(). .getcode() ? , , url, - , 404.

+4
2

404 urllib2, , , . getcode():

>>> import urllib2
>>> try:
...     url = urllib2.urlopen('http://www.wired.com/tag/magazine-23-05/page/4')
... except urllib2.HTTPError, e:
...     print e
...     print e.getcode()
...
HTTP Error 404: Not Found
404

, , . URL ( urllib2.build_opener(), urllib2.install_opener()), urllib2.HTTPErrorProcessor .

- 2xx, . 3xx HTTPRedirectHandler, 40x ( ) , .

Python, requests library, , , :

import requests

response = requests.get(url)
response.raise_for_status()  # raises an exception for 4xx or 5xx status codes.
+4

, , "" http. , , , URL- , URL- , 404 , URL- .

urllib2.urlopen (), , http (. urllib2.HTTPError)

, requests, , .

0

All Articles