I have a list of urls. I'd like to see the server response code of each and find out if any are broken. I can read server errors (500) and broken links (404) okay, but the code breaks once a non-website is read (e.g. "notawebsite_broken.com"). I've searched around and not found the answer... I hope you can help.
Here's the code:
import urllib2
#List of URLs. The third URL is not a website
urls = ["http://www.google.com","http://www.ebay.com/broken-link",
"http://notawebsite_broken"]
#Empty list to store the output
response_codes = []
# Run "for" loop: get server response code and save results to response_codes
for url in urls:
try:
connection = urllib2.urlopen(url)
response_codes.append(connection.getcode())
connection.close()
print url, ' - ', connection.getcode()
except urllib2.HTTPError, e:
response_codes.append(e.getcode())
print url, ' - ', e.getcode()
print response_codes
This gives the output of...
http://www.google.com - 200
http://www.ebay.com/broken-link - 404
Traceback (most recent call last):
File "test.py", line 12, in <module>
connection = urllib2.urlopen(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1214, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1184, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>
Does anyone know a fix for this or can anyone point me in the right direction?