关于在使用except异常递归调用函数时出现的返回值为空

刚开始在 except Exception as e:  中直接写的get_token(),
结果第一次执行,无值取[0],报错进入异常,
然后多次之后,有值,跳出函数 return oohTokenName, oohTokenValue
oohTokenName, oohTokenValue = get_token() 却为空,
需要在except Exception as e: 修改为   return get_token()
def get_token():
    url = 'https://www.xxxxx.com/user/login'

    html = session.get(url, headers=headers).text
    print(html)

    try:
        print('>>>>>>>>>', re.findall('ooh.token.name" value="(.*?)"', html))
        print('>>>>>>>>>', re.findall('ooh.token.value" value="(.*?)"', html))

        oohTokenName = re.findall('ooh.token.name" value="(.*?)"', html)[0]
        oohTokenValue = re.findall('ooh.token.value" value="(.*?)"', html)[0]
        return oohTokenName, oohTokenValue

    except Exception as e:
        print(f"An error occurred: {e}, retrying in 3 seconds...")
        time.sleep(3)

        # 这里需要 return get_token()
        # 不能直接写成 get_token()
        return get_token()


oohTokenName, oohTokenValue = get_token()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值