0

i am a completely beginner in programming, and i am trying to make my first python script, and i have a url in this form: https://stackoverflow.com/questions/ID

where ID is changed every time in a loop, and a list of IDs given in a text file.

now i tried to do it this way:

if id_str != "":

     y = f'https://stackoverflow.com/questions/{id}'

     browser.get(y)

but it opens only the first ID in the text file, so i need to know how to make it get a different ID from the text file every time.

Thanks in Advance

1 Answer 1

2

Generally it can be something like this:

with open(filename) as file:
    lines = file.readlines()
    for line in lines:
        if id_str != "":
            y = f'https://stackoverflow.com/questions/{id_str}'
            browser.get(y)

where filename is a text file containing questions ids.
Each line here containing a single id string.
It can be more complicated, according to your needs / implementation.

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

3 Comments

I believe the placeholder in the formatted string should be id_str and not id
it says: name 'ids' is not defined ids.txt is the file name
Where do you see ids variable in my code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.