Разбор ввода текстового файла в python - PullRequest
0 голосов
/ 24 апреля 2020

У меня проблемы с чтением ввода из текстового файла. Также я заметил, что только последняя строка читается и анализируется.

out1.txt:

thin279
gatefin
64hamp
testme

Код:

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)

Ниже выводится только последний элемент в списке

=========== 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
>>> 

Поэтому я хочу знать, как получить его для всех идентификаторов пользователей, а не только для последнего (testme).

Ответы [ 2 ]

1 голос
/ 24 апреля 2020

Содержание второго for l oop должно быть внутри первого, или вам нужно изменить первый, чтобы сохранить все идентификаторы в списке. В настоящее время вы просто просматриваете все значения в текстовом файле, затем начинаете новый l oop, который просто использует значение, оставленное в конце исходного l oop.

0 голосов
/ 25 апреля 2020

Это код, который работал из руководства @SimonN и справки.

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)

...