python 中try 和except的用法

#try表示去打开一个网页,看是否有错误,注意Exception的书写

#觉得例子是爬豆瓣网5次

import re
import urllib.request
for i in range(1,5):
    try:
        file = urllib.request.urlopen("https://read.douban.com/provider/all",timeout = 0.5)
        data = file.read()
        print(len(data))
    except Exception as e:
        print("出现异常:"+str(e))

猜你喜欢

转载自blog.csdn.net/weixin_42099676/article/details/81319248