我想处理错误:
“httperror_seek_wrapper:HTTP错误404:未找到”
而只是返回一个空字符串。但我不确定除了陈述应该是什么。如果有重复的帖子,我很抱歉,但是没有一个帖子突然出现,除了这个错误,而是找出更深层次出错的地方。但是,我只想传递并返回一个空字符串。
基本上,我正在寻找应该填写的内容:
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
答案 0 :(得分:3)
这是urllib2.HTTPError
例外。
from urllib2 import HTTPError
try:
# ...
except HTTPError:
# ...
您可以在source of _mechanize.py
中看到此内容,其中捕捉到相同的异常并将其分配给response
以便稍后重新提升。