python的异常处理及支持中文注释

本文介绍如何使用Python的urllib库访问网站并获取内容,通过尝试-异常处理机制确保程序稳定运行,并解决中文注释问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      初次玩urllib库,访问一个网站: http://www.busge.com/,没承想却遭遇了狙击。

      类似于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语句正确运行后,才能运行此处的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

proware

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值