python-此错误的例外情况是:httperror_seek_wrapper:HTTP错误404:找不到?
作者:互联网
我想处理错误:
“ httperror_seek_wrapper:HTTP错误404:未找到”
而是只返回一个空字符串.但是我不确定except语句应该是什么.如果出现重复的帖子,我深表歉意,但是弹出的所有帖子似乎都没有处理该错误,而是在更深层次上弄清楚出了什么问题.但是,我只想传递并返回一个空字符串.
基本上,我正在寻找空白内容:
try:
....
except _____:
....
谢谢!
_____
这是引发错误的代码.
---------------------------------------------------------------------------
httperror_seek_wrapper Traceback (most recent call last)
<ipython-input-181-e2b68bf19ff9> in <module>()
5
6 for i in range(len(first)):
----> 7 student_html = get_stud_html(first[i],last[i],'Sophomore')
8 # print type(student_html)
9 # print parse_hfb_page(student_html)
<ipython-input-177-9b1d4294820d> in get_stud_html(first, last, year)
60 #ideally will want to use a regex to do this in case there's more than one link on the page
61 stud_url = 'http://facebook.college.harvard.edu/'+links_lst[12]
---> 62 stud_html = br.open(stud_url)
63 return stud_html.get_data()
64
//anaconda/python.app/Contents/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.pyc in open(self, url, data, timeout)
201 def open(self, url, data=None,
202 timeout=_sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
--> 203 return self._mech_open(url, data, timeout=timeout)
204
205 def _mech_open(self, url, data=None, update_history=True, visit=None,
//anaconda/python.app/Contents/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.pyc in _mech_open(self, url, data, update_history, visit, timeout)
253
254 if not success:
--> 255 raise response
256 return response
257
httperror_seek_wrapper: HTTP Error 404: Not Found
解决方法:
那是一个urllib2.HTTPError异常.
from urllib2 import HTTPError
try:
# ...
except HTTPError:
# ...
您可以在source of _mechanize.py
中看到这一点,在该处捕获了相同的异常并将其分配给响应,以便稍后重新引发.
标签:error-handling,python 来源: https://codeday.me/bug/20191122/2059437.html