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.