我的意思是想说,当我用Urllib2.urlopen()打开网页失败或错误的时候,自动放弃打开这个网页,而执行其它命令。不会因为这个页面打开失败而终止整个程序的执行。请问改用什么异常函数来捕获这种网页打开异常。
try:
htmlContent = urllib2.urlopen(URL).read()
except 该怎么写,谢谢?
有HTTPError吗?程序运行显示该异常未定义。
追答当然有,你应该多看文档。
exception urllib2.HTTPError
Though being an exception (a subclass of URLError), an HTTPError can also function as a non-exceptional file-like return value (the same thing that urlopen() returns). This is useful when handling exotic HTTP errors, such as requests for authentication.
我的是Python 2.7的。
你这个是不是Python 3的?
这是2.7的文档
追问请问去哪看这些文档呢?我刚学不久
追答Python 官网,或者其他镜像的文档。
比如说
http://docs.python.org/2/library/urllib2.html?highlight=urllib2.httperror#urllib2.HTTPError