The following code executes as expected. However, the break function doesn't stop my code from executing and continues in an endless loop. How can I stop the operation from running and exit after break
? The goal is to continue with the script every 60 seconds if the text isn't found on the page. If the text is found, perform some operations and exit.
while True:
#operation to scrape data from page
while True:
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
startTime = '11:59:00'
#if it's before 11:59
if current_time < startTime:
# if 9:30 doesn't appear on the page, wait 60 and try again.
if str(soup).find("9:30") == -1:
# wait 60 seconds,
time.sleep(60)
# continue with the script,
continue
else:
#9:30 Appears on the page so continue
#PerformFunction()
print('Time found. Performing operation...')
time.sleep(10)
break
else:
# It's after 11:59
#if 4:30 doesn't appear on the page try again in 60
if str(soup).find("4:30") == -1:
# wait 60 seconds,
time.sleep(60)
# continue with the script,
continue
else:
#4:30 Appears on the page so continue
#PerformFunction()
print('Time found. Performing operation...')
time.sleep(10)
break
I've also tried adding a boolean check:
checker = True
while True and checker:
#operation to scrape data from page
while True:
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
startTime = '11:59:00'
#if it's before 11:59
if current_time < startTime:
# if 9:30 doesn't appear on the page, wait 60 and try again.
if str(soup).find("9:30") == -1:
# wait 60 seconds,
time.sleep(60)
# continue with the script,
continue
else:
#9:30 Appears on the page so continue
#PerformFunction()
print('Time found. Performing operation...')
time.sleep(10)
checker = False
break
#I've also tried return in the place of break but get SyntaxError
else:
# It's after 11:59
#if 4:30 doesn't appear on the page try again in 60
if str(soup).find("4:30") == -1:
# wait 60 seconds,
time.sleep(60)
# continue with the script,
continue
else:
#4:30 Appears on the page so continue
#PerformFunction()
print('Time found. Performing operation...')
time.sleep(10)
checker = False
break
#I've also tried return in the place of break but get SyntaxError