美文网首页python自学Python原教Python爬虫作业
Python爬虫day3.2—python异常处理

Python爬虫day3.2—python异常处理

作者: techLee | 来源:发表于2017-12-26 17:15 被阅读11次

    异常处理概述

    python程序在执行的时候,经常会遇到异常,如果中间异常不处理,经常会导致程序崩溃。比如爬虫,如果不进行异常处理,很可能虫爬了一半,直接崩溃了。

    #异常处理
    for i in range(0,10):
        print(i)
        if(i==4):
            print(iuuuu)
    
    代码运行异常
    异常处理格式:
    try:
        程序
    except Exception as 异常名称:
        异常处理部分
    
    #异常处理
    '''
    #异常处理
    '''
    异常处理格式:
    try:
        程序
    except Exception as 异常名称:
        异常处理部分
    '''
    
    try:
        for i in range(0,10):
            print(i)
            if(i==4):
                print(iuuuu)
        print("hello")        
    
    except Exception as err:
        print(err)
    
    代码运行结果
    #让异常后的程序继续
    for i in range(0,10):
        try:
            print(i)
            if(i==4):
                print(iuuuu)
        except Exception as err:
            print(err)
    print("hello") 
    
    代码运行结果

    注意代码缩进
    注意代码缩进
    注意代码缩进

    相关文章

      网友评论

        本文标题:Python爬虫day3.2—python异常处理

        本文链接:https://www.haomeiwen.com/subject/kadjgxtx.html