0

I am having problems reading input from text file. Also I noticed that only the last line is read and parsed.

out1.txt:

thin279
gatefin
64hamp
testme

Code:

import time
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")


with open('out1.txt') as f:
    for line in f:
        #do not forget to strip the trailing new line
        userid = line.rstrip("\n")
        print (userid)
for i in range (len(userid)):
    ucheck = ("https://example.com/?profile=" + userid)
    xucheck = webdriver.Chrome(options=chrome_options)
    xucheck.get(ucheck)
    print (ucheck)

Below is the output showing only the last item in the list

=========== RESTART: C:/Users/Administrator/Desktop/project_3/qyes.py ==========
thin279
gatefin
64hamp
testme
https://example.com/?profile=testme
https://example.com/?profile=testme
https://example.com/?profile=testme
https://example.com/?profile=testme
https://example.com/?profile=testme
https://example.com/?profile=testme
>>> 

So I want to know how to get it to all the userid and not just the last one (testme).

2 Answers 2

1

The content of the second for loop needs to be inside the first one, or you need to change the first one to store all the userids to a list. Currently you are just looping through all the values in the text file, then starting a new loop which is just using the value left at the end of the original loop.

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

2 Comments

Thanks @SimonN, I corrected the indentation and it prints all but it repeats each userid for 6 times before jumping to the next.
You need to do away with the second loop altogether, you only need the contents. It's printing 6 times because you are looping over the length of each userid (ie six characters) which is completely unnecessary.
0

This is the code that worked from @SimonN guide and help.

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")


with open('out1.txt') as f:
    for line in f:
        #do not forget to strip the trailing new line
        userid = line.rstrip("\n")
        print (userid)
        ucheck = ("https://example.com/?profile=" + userid)
        xucheck = webdriver.Chrome(options=chrome_options)
        xucheck.get(ucheck)
        print (ucheck)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.