初次玩urllib库,访问一个网站:,没承想却遭遇了狙击。

      类似于java等语言,python也采用try - except 监控及处理可能发生的错误。

     同时在 python中遇到中文注释问题,参考官方资料解决: ​​ 增加中文注释的方法​​    根据此文,在代码开始处添加:  # coding=utf-8

    (1)   代码如下,很简单,就是将网页首页的内容获取下来

import urllib2
# create a request
url="http://www.busge.com/"
request=urllib2.Request(url)

# build a connection and send the request,then get the response
response=urllib2.urlopen(request)
print e.reason

# show response
print response.read()


    但是运行时,却出现了错误,按照C语言的思路,如下两个红色部分之间看起来像个调用链,而只有第一个红色语句是本人所写的代码,其余的都是库里的类接口实现。

Traceback (most recent call last):
File "url.py", line 6, in <module>
response=urllib2.urlopen(request)
File "/usr/lib/python2.6/urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.6/urllib2.py", line 395, in open
response = meth(req, response)
File "/usr/lib/python2.6/urllib2.py", line 508, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.6/urllib2.py", line 433, in error
return self._call_chain(*args)
File "/usr/lib/python2.6/urllib2.py", line 367, in _call_chain
result = func(*args)
File "/usr/lib/python2.6/urllib2.py", line 516, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error

   

 (2)  而后赶紧修改代码,增加try-except语句

   

# build a connection and send the request,then get the response
try:
response=urllib2.urlopen(request)
except urllib2.URLError, e:
print e.reason

# show response
print response.read()

修改后的结果为下面内容,不再有那长长的看不懂的函数调用了,而只有一句错误提示及另外一个运行错误。


Internal Server Error


Traceback (most recent call last):


  File "url.py", line 14, in <module>


    print response.read()


NameError: name 'response' is not defined


  (3) 继续修改代码:增加else

   

# build a connection and send the request,then get the response
try:
response=urllib2.urlopen(request)
except urllib2.URLError, e:
print e.reason
else:
# show response
print response.read()


 运行结果:

      Internal Server Error


     综上,对于可能发生错误的语句(诸如打开文件失败,打开连接失败等),要加try语句进行检查;同时为了明确错误,需要except解析可能的错误原因。对于后续依赖于try语句块中的代码,需要用else关键字标识,以保证在try语句正确运行后,才能运行此处的代码。