处理 wget 模块中的 404 未找到错误 python
Handling 404 not found error in wget module python
x=input('Enter Roll Number :')
str1 = "http://xxxxx/xxxx/students_uploads/"
str2 = x+"_P.jpg"
res = str1 + str2
filename = wget.download(res)
这个简单的代码将从我们学院下载学生的个人资料图片 website.The 如果我们输入正确的学生卷号,代码运行良好,这意味着该网站有一个关于该卷号的文件,但是当我们输入错误的卷号时它抛出
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
当有人输入错误的 rollnumber 时,有什么方法可以简单地将输出输出到 File not found enter correct roll number 。
谢谢
围绕 wget.download
调用的简单异常捕获应该足够了:
try:
filename = wget.download(res)
except Exception as exc:
print(f"wget failed: {str(exc)}")
x=input('Enter Roll Number :')
str1 = "http://xxxxx/xxxx/students_uploads/"
str2 = x+"_P.jpg"
res = str1 + str2
filename = wget.download(res)
这个简单的代码将从我们学院下载学生的个人资料图片 website.The 如果我们输入正确的学生卷号,代码运行良好,这意味着该网站有一个关于该卷号的文件,但是当我们输入错误的卷号时它抛出
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
当有人输入错误的 rollnumber 时,有什么方法可以简单地将输出输出到 File not found enter correct roll number 。 谢谢
围绕 wget.download
调用的简单异常捕获应该足够了:
try:
filename = wget.download(res)
except Exception as exc:
print(f"wget failed: {str(exc)}")