Home > other >  In a try/except using nested functions, why has entered the except the nested function, also continu
In a try/except using nested functions, why has entered the except the nested function, also continu

Time:10-10

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

Try:
The req=requests. Get (url, headers=headers)
The req. Encoding="utf-8"
HTML=the req. Text
Except requests. Exceptions. ConnectionError:
Print (" retry the line on the Internet...
")Rank_us (asin, site)
Html_x=etree. HTML (HTML)



The code above, when conditions, implementation to the except the rank_us function inside the
Then an error,
UnboundLocalError: local variable 'HTML' referenced before the assignment

Recently met a lot of this problem, the nested function, but not over the execution of the current...

To solve the

CodePudding user response:

What do you want to do?

You try the except just catch exceptions network connection, other exceptions not capture,

Look at the code, it should be abnormal, HTML is no assignment,
To html_x=etree. HTML (HTML) will make a mistake here,

CodePudding user response:

This can capture UnboundLocalError abnormalities,
 


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

Try:
The req=requests. Get (url, headers=headers)
The req. Encoding="utf-8"
HTML=the req. Text
Except requests. Exceptions. ConnectionError:
Print (" retry the line on the Internet...
")Rank_us (asin, site)
Try:
Html_x=etree. HTML (HTML)
Except the Exception as e:
Print (" error ")
Print (repr (e))


CodePudding user response:

refer to the second floor seakingx response:
this can capture UnboundLocalError abnormalities,
 


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

Try:
The req=requests. Get (url, headers=headers)
The req. Encoding="utf-8"
HTML=the req. Text
Except requests. Exceptions. ConnectionError:
Print (" retry the line on the Internet...
")Rank_us (asin, site)
Try:
Html_x=etree. HTML (HTML)
Except the Exception as e:
Print (" error ")
Print (repr (e))




I mean here, that is, network connection exception handling reconnection, then using the nested function to reconnect
I here also print (" retry the line on the Internet "), is entered my nested function, would not be for the following
Html_x=etree. HTML (HTML) # here the cause of the error of the exception because the above HTML, didn't get HTML, i.e., HTML undefined is invoked so error



But the problem is coming, the network connection is unusual, get into nested reconnect? Why will perform
Html_x=etree. HTML (HTML) #
To cause an error

CodePudding user response:

refer to the second floor seakingx response:
this can capture UnboundLocalError abnormalities,
 


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

Try:
The req=requests. Get (url, headers=headers)
The req. Encoding="utf-8"
HTML=the req. Text
Except requests. Exceptions. ConnectionError:
Print (" retry the line on the Internet...
")Rank_us (asin, site)
Try:
Html_x=etree. HTML (HTML)
Except the Exception as e:
Print (" error ")
Print (repr (e))




UnboundLocalError: local variable 'HTML' referenced before the assignment
This error is that we haven't define variables of the calling

CodePudding user response:

Try the except is a block,
Html_x=etree. HTML (HTML)

In the try the except block, try the except the situation does not affect html_x=etree. HTML (HTML),



You can put the html_x=etree. HTML (HTML) on the block, or add logo, judgment and then perform html_x=etree. HTML (HTML)


 


Def rank_us (asin, site) :
.
Url="https://www.amazon.com/dp/" + asin
Ret_flag=False
Try:
The req=requests. Get (url, headers=headers)
The req. Encoding="utf-8"
HTML=the req. Text
Ret_flag=True
Except requests. Exceptions. ConnectionError:
Print (" retry the line on the Internet...
")Rank_us (asin, site)
If ret_flag:
Html_x=etree. HTML (HTML)

CodePudding user response:

To such conservatively, catch all exceptions,
 


Def rank_us (asin, site) :
.
Url="https://www.amazon.com/dp/" + asin
Ret_flag=False
Try:
The req=requests. Get (url, headers=headers)
The req. Encoding="utf-8"
HTML=the req. Text
Ret_flag=True
Except the Exception as e:
Print (" error ")
Print (repr (e))
Rank_us (asin, site)
If ret_flag:
Html_x=etree. HTML (HTML)


CodePudding user response:

refer to 6th floor seakingx response:
change to such conservatively, catch all exceptions,
 


Def rank_us (asin, site) :
.
Url="https://www.amazon.com/dp/" + asin
Ret_flag=False
Try:
The req=requests. Get (url, headers=headers)
The req. Encoding="utf-8"
HTML=the req. Text
Ret_flag=True
Except the Exception as e:
Print (" error ")
Print (repr (e))
Rank_us (asin, site)
If ret_flag:
Html_x=etree. HTML (HTML)




A little carried away, but still thank you brother
Mainly in the nested function, why still continues, don't know the price return will better

And except the Exception as e:
This, in fact, there are many exceptions are not capture, such as selenium module inside would have a lot of can't catch exceptions
The need to determine the except the selenium. * * custom capture the error types in order to capture
  • Related