Im trying to code a script where I check a certain page of names. Before I continue summarizing the problem. The page is known to flick - meaning that once you go to the page it can list you names. The next refresh page you do, it will return empty name list and the next one again it will list you the names again. (That's the further idea I am trying to do) however I have created a little own script where we as user can test it easier.
Instead of requests, I have created a txt file to easier run the program
What I am trying to do is following:
I want to make the script so it opens the txt every loop, it checks if there is names in the list, then we print it out only once. And if there is no names - Then I want to make a counter that checks if the names actually are empty or not, meaning in that case I want to create a sort of counter that confirms that, and declare that there is no names in the list. Meaning that after 5 opens of txt file and there haven't been any names in the list after those 5 in the row opening. then we can declare it as its actually empty.
If counter have confirmed that it is empty, then we loop until we find names and print it once again and then we start over as previous.
What I have tried is that I think there is slight problem when I am coding where I might confuse myself or over-complicate myself more than I actually should.
count = 0
last_names = []
names_checker = False
while True:
with open('./test.txt') as f:
new_product_values = json.load(f)
# {'name': 'Random names', 'url': 'www.stackoverflow.com', 'names': []}
if names_checker == False:
if not new_product_values['sizes']:
count += 1
time.sleep(1)
if count == 5:
names_checker = True
count = 0
logger.success('Declare value as No names')
else:
names_checker = True
elif names_checker == True:
while True:
if new_product_values['names'] != last_names:
print("NEW NAMES!")
print(new_product_values['names'])
last_names = new_product_values['names']
logger.status(last_names)
names_checker = False
break
else:
logger.warn("Nothing found - sleep 1")
time.sleep(1)
text file:
{'name': 'Random names', 'url': 'www.stackoverflow.com', 'names': []}
My expected results in that case is that:
If there is no names in the list, we add one in the counter, if the next loop still continues to give us empty name then we add another one in the counter until it hits the counter 5, when it hits counter 5 I want it to declare it as the list is empty. Whenever it is empty, we want to loop until we find names. And once we find the names we want to declare that the list is not empty and print out the names once and start over with the counter over again.