class LiFailed(): pass class TrFailed(): pass try: # whole code of li structure if a == " ": raise LiFailed except LiFailed: try: # whole code of tr if b == " ": raise TrFailed except TrFailed: try: # your code to exhaustive search of word "Shipping Weight"