In Python, how do I use urllib to see if a website is 404 or 200?
如何通过urllib获取报头的代码?
getcode()方法(添加在python2.6中)返回随响应发送的HTTP状态代码,如果URL不是HTTP URL,则返回none。
1 2 3 4 5 6 | >>> a=urllib.urlopen('http://www.google.com/asdfsf') >>> a.getcode() 404 >>> a=urllib.urlopen('http://www.google.com/') >>> a.getcode() 200 |
您也可以使用urllib2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import urllib2 req = urllib2.Request('http://www.python.org/fish.html') try: resp = urllib2.urlopen(req) except urllib2.HTTPError as e: if e.code == 404: # do something... else: # ... except urllib2.URLError as e: # Not an HTTP-specific error (e.g. connection refused) # ... else: # 200 body = resp.read() |
注意,
对于Python 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import urllib.request, urllib.error url = 'http://www.google.com/asdfsf' try: conn = urllib.request.urlopen(url) except urllib.error.HTTPError as e: # Return code error (e.g. 404, 501, ...) # ... print('HTTPError: {}'.format(e.code)) except urllib.error.URLError as e: # Not an HTTP-specific error (e.g. connection refused) # ... print('URLError: {}'.format(e.reason)) else: # 200 # ... print('good') |
1 2 3 4 5 6 7 8 | import urllib2 try: fileHandle = urllib2.urlopen('http://www.python.org/fish.html') data = fileHandle.read() fileHandle.close() except urllib2.URLError, e: print 'you got an error with the code', e |