0

I have a question regarding a script that I am writing. The problem is indicated in: #FIXME comment,start_string should be inserted only once but it kept inserting after each iteration.

script.py:

NUMBER_OF_PAGES_TO_SEARCH = 50 #change this if you want to go over more pages

page = NUMBER_OF_PAGES_TO_SEARCH

start_string = '&start='
while True:
    if page != 0 :
        try:
            #FIXME: start_string is inserted after each loop, page decrement is working correctly

            driver.get(driver.current_url + start_string +str(page))
        except:
            break
        
        time.sleep(7)
            
    # print(page_to_count)
    page = page - 10
    if page == 0:
        break

    print(page)

3
  • 1
    You mean, after each iteration of a loop the start_string is inserted? Please post the whole for loop so we can see what you are iterating over. Commented Nov 21, 2020 at 15:15
  • @xcoding Please share all of your code..and the error. Commented Nov 21, 2020 at 15:15
  • Just use a for loop instead of while Commented Nov 21, 2020 at 15:27

2 Answers 2

1

Do you want to execute the start_string only once? and then only change the page number? Then we can use a boolean flag. Initialize the flag=False before the while loop, and use if-else inside your try block, something like this :

if (not flag):
     driver.get(driver.current_url + start_string +str(page))    
else:
     driver.get(driver.current_url +str(page))
flag=True

This code will execute the driver.get(driver.current_url + start_string +str(page)) only once and then it will keep executing the else block.

Sign up to request clarification or add additional context in comments.

4 Comments

tried this but i only got (current url) and decremented numbers, no (start_string)
As you mentioned in the question that "looking for a way to keep start_string from not inserting each time", so i thought you want start_string to be inserted only once, so exactly what output are you expecting here?
output expected should be : (current_url&start=50), once loop restarted (50 will be decremented to 40) etc, but not (current_url&start=50l&start=40) etc
managed to insert start_string once, but got another problem, when &start=50, inserted, the decremented number not changing instead this is the url output &start=504030 etc..
0

your use of a while loop is just outputting the same result as is inputted. try instead a for loop so the code doesn't automatically insert its self every time it runs through.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.