python - urllib2.Request check if URL is reachable -
Then I have the following code to correct some URL, I just need 200 responses, so I have a script But it's very slow (imported:
import urllib2 import string def my_range (start, end, phase): while starting In this example I think that the following directories in my local hosts are with this result
http: // example com / test / 2 200 Http://exemple.com/test/3 200 http://exemple.com/test/4 404 http://exemple.com/test/5 404 http://exemple.com/ Test / 6 404 So I came to know how quickly I get this code:
import urll Ib2 request = urllib2.Request ('http: // www .google.com / ') response = urllib2.u Rlopen (request) if response.getcode () == 200: print "200" It looks sharp, but when I check it like (404) it gives me this Returns the result:
Traceback (last most recent call): The file "C: \ Python27 \ res. Py ", line 3, & lt; module & gt; response = urllib2.urlopen (request) file" C: \ Python27 \ lib \ urllib2.py ", line 126, urlopen return _opener.open (url, data, timeout) File "C: \ Python27 \ lib \ urllib2.py", line 513, http_response 'http' in the file "C: \ Python27 \ lib \ urllib2.py", line 400, open response = meth (req, response) The file "C: \ Python27 \ lib \ urllib2.py", error 438, error in the self._call_chain (* args) file "C: \ Python27 \ lib \ urllib2.py", "C: \ Python27 \ lib \ urllib2.py", request, response, code, message, , Line 372, _call_chain result = in the func (* args) file "C: \ Python27 \ lib \ urllib 2.py ", line 521, HTTPError (req.get_full_url (), code, msg, hdrs, fp) urllib2.HTTP error in http_error_default: HTTP Error 404: Not Found Any People with thoughts and thank you very much for any help :)
HTTPError is defined as a family of exceptions so you can try / put in such cases: import urllib2 request = urllib.request ('http: / /www.google.com/ '): Feedback = Urllib.urlopen (requested) # do stuff .. urllib2.HTTP Uti: # 404, in addition to 500, and so on. Close to You can add a > code >> urllib2.URLError for clauses which contain other (non-HTTP) errors Like timeout
Comments
Post a Comment