Home > Mobile >  Handling 404 not found error in wget module python
Handling 404 not found error in wget module python

Time:02-23

x=input('Enter Roll Number :')
str1 = "http://xxxxx/xxxx/students_uploads/"
str2 = x "_P.jpg"
res = str1   str2
filename = wget.download(res)

This simple code will download students profile pictures from our college website.The code works well if we entered correct roll number of student which means the website had a file on that roll number but when we enter wrong roll number it throws

Exception                                 Traceback (most recent call last)
<ipython-input-20-cbbda742e201> in <module>
      4 str2 = x "_P.jpg"
      5 res = str1   str2
----> 6 filename = wget.download(res)

D:\Anaconda\lib\site-packages\wget.py in download(url, out, bar)
    314         callback = None
    315 
--> 316     (tmpfile, headers) = ThrowOnErrorOpener().retrieve(url, tmpfile, callback)
    317     names["header"] = filename_from_headers(headers)
    318     if os.path.isdir(names["out"]):

D:\Anaconda\lib\urllib\request.py in retrieve(self, url, filename, reporthook, data)
   1822             except OSError as msg:
   1823                 pass
-> 1824         fp = self.open(url, data)
   1825         try:
   1826             headers = fp.info()

D:\Anaconda\lib\urllib\request.py in open(self, fullurl, data)
   1788         try:
   1789             if data is None:
-> 1790                 return getattr(self, name)(url)
   1791             else:
   1792                 return getattr(self, name)(url, data)

D:\Anaconda\lib\urllib\request.py in open_http(self, url, data)
   1966     def open_http(self, url, data=None):
   1967         """Use HTTP protocol."""
-> 1968         return self._open_generic_http(http.client.HTTPConnection, url, data)
   1969 
   1970     def http_error(self, url, fp, errcode, errmsg, headers, data=None):

D:\Anaconda\lib\urllib\request.py in _open_generic_http(self, connection_factory, url, data)
   1960                               response.status)
   1961         else:
-> 1962             return self.http_error(
   1963                 url, response.fp,
   1964                 response.status, response.reason, response.msg, data)

D:\Anaconda\lib\urllib\request.py in http_error(self, url, fp, errcode, errmsg, headers, data)
   1982                 result = method(url, fp, errcode, errmsg, headers, data)
   1983             if result: return result
-> 1984         return self.http_error_default(url, fp, errcode, errmsg, headers)
   1985 
   1986     def http_error_default(self, url, fp, errcode, errmsg, headers):

D:\Anaconda\lib\site-packages\wget.py in http_error_default(self, url, fp, errcode, errmsg, headers)
    285 class ThrowOnErrorOpener(urllib.request.FancyURLopener):
    286     def http_error_default(self, url, fp, errcode, errmsg, headers):
--> 287         raise Exception("{0}: {1}".format(errcode, errmsg))
    288 
    289 def download(url, out=None, bar=bar_adaptive):

Exception: 404: Not Found

Is there any way where i can simply put the output to File not found enter correct roll number when someone inputs wrong rollnumber . Thanks.

CodePudding user response:

Simple exception catch should be enough around the wget.download call:

try:
    filename = wget.download(res)
except Exception as exc:
    print(f"wget failed: {str(exc)}")
  • Related