在try/except中使用嵌套函数,为什么进入了except中的嵌套函数,还继续执行后续的函数的?

cool_soup29 2019-11-29 10:15:20

def rank_us(asin,site):
...
url = "https://www.amazon.com/dp/" + asin

try:
req=requests.get(url,headers=headers)
req.encoding='utf-8'
html=req.text
except requests.exceptions.ConnectionError:
print("retry line the internet...")
rank_us(asin,site)
html_x = etree.HTML(html)



以上代码,几时条件符合,执行到了except 中的 rank_us函数里面
然后报错,
UnboundLocalError: local variable 'html' referenced before assignment

最近遇到好多这个问题,嵌套函数执行了,却没有结束当前的执行....

求解
...全文
158 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈年椰子 2019-11-29
  • 打赏
  • 举报
回复
这样能捕获UnboundLocalError 的异常。



def rank_us(asin,site):
    ...
    url = "https://www.amazon.com/dp/" + asin
 
    try:
        req=requests.get(url,headers=headers)
        req.encoding='utf-8'
        html=req.text
    except requests.exceptions.ConnectionError:
        print("retry line the internet...")
        rank_us(asin,site)
    try:
        html_x = etree.HTML(html)
    except Exception as e:
        print("出错啦 ")
        print(repr(e))


陈年椰子 2019-11-29
  • 打赏
  • 举报
回复
你想做什么? 你的 try except 只是捕获 网络连接异常 。 其他异常不捕获。 看代码, 应该是 出现异常了, html 没有赋值, 到 html_x = etree.HTML(html) 这里就出错了。
cool_soup29 2019-11-29
  • 打赏
  • 举报
回复
引用 6 楼 seakingx 的回复:
改成这样稳妥些, 把所有异常都捕获。



def rank_us(asin,site):
    ...
    url = "https://www.amazon.com/dp/" + asin
    ret_flag = False
    try:
        req=requests.get(url,headers=headers)
        req.encoding='utf-8'
        html=req.text
        ret_flag = True
    except Exception as e :
        print("出错啦 ")
        print(repr(e))
        rank_us(asin,site)
    if ret_flag:
        html_x = etree.HTML(html)


有点跑题啦,不过还是谢谢老哥 主要是嵌套函数的,为何还继续进行下去的,不知道价格return 会不会好点 还有except Exception as e: 这个,其实有很多异常是无法捕捉的,例如的话selenium模块里面就已经有很多无法捕捉的异常 需要自定 except selenium.**自定义捕捉他报错的类型才能捕捉到
陈年椰子 2019-11-29
  • 打赏
  • 举报
回复
改成这样稳妥些, 把所有异常都捕获。



def rank_us(asin,site):
    ...
    url = "https://www.amazon.com/dp/" + asin
    ret_flag = False
    try:
        req=requests.get(url,headers=headers)
        req.encoding='utf-8'
        html=req.text
        ret_flag = True
    except Exception as e :
        print("出错啦 ")
        print(repr(e))
        rank_us(asin,site)
    if ret_flag:
        html_x = etree.HTML(html)


陈年椰子 2019-11-29
  • 打赏
  • 举报
回复
try except 就是个块, html_x = etree.HTML(html) 在 try except 块 外, try except 的情况 不影响 html_x = etree.HTML(html) 的执行。 你可以把 html_x = etree.HTML(html) 放到块里, 或者加 标识 ,判断后再执行 html_x = etree.HTML(html)



def rank_us(asin,site):
    ...
    url = "https://www.amazon.com/dp/" + asin
    ret_flag = False
    try:
        req=requests.get(url,headers=headers)
        req.encoding='utf-8'
        html=req.text
        ret_flag = True
    except requests.exceptions.ConnectionError:
        print("retry line the internet...")
        rank_us(asin,site)
    if ret_flag:
        html_x = etree.HTML(html)

cool_soup29 2019-11-29
  • 打赏
  • 举报
回复
引用 2 楼 seakingx 的回复:
这样能捕获UnboundLocalError 的异常。



def rank_us(asin,site):
    ...
    url = "https://www.amazon.com/dp/" + asin
 
    try:
        req=requests.get(url,headers=headers)
        req.encoding='utf-8'
        html=req.text
    except requests.exceptions.ConnectionError:
        print("retry line the internet...")
        rank_us(asin,site)
    try:
        html_x = etree.HTML(html)
    except Exception as e:
        print("出错啦 ")
        print(repr(e))


UnboundLocalError: local variable 'html' referenced before assignment 这个错误的是我们的变量还没定义就进行调用了
cool_soup29 2019-11-29
  • 打赏
  • 举报
回复
引用 2 楼 seakingx 的回复:
这样能捕获UnboundLocalError 的异常。



def rank_us(asin,site):
    ...
    url = "https://www.amazon.com/dp/" + asin
 
    try:
        req=requests.get(url,headers=headers)
        req.encoding='utf-8'
        html=req.text
    except requests.exceptions.ConnectionError:
        print("retry line the internet...")
        rank_us(asin,site)
    try:
        html_x = etree.HTML(html)
    except Exception as e:
        print("出错啦 ")
        print(repr(e))


我这里的本意呢,就是说处理网络连接异常重连的,然后就用嵌套函数重新连接 我这里也print("retry line the internet")了,也就是进入了我的嵌套函数里面,就不会进行下面的 html_x = etree.HTML(html) #这里报错的原因的是因为上面的获取html异常了嘛,没有得到html,也就是html未定义就进行调用所以报错 但是问题来了, 网络连接异常,就进入嵌套重新连接了吧?为何还会执行 html_x = etree.HTML(html) # 再引起错误呢

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧