Python 3.8 用python解析文本文件输入
我在从文本文件读取输入时遇到问题。我还注意到只有最后一行被读取和解析 out1.txt:Python 3.8 用python解析文本文件输入,python-3.8,Python 3.8,我在从文本文件读取输入时遇到问题。我还注意到只有最后一行被读取和解析 out1.txt: thin279 gatefin 64hamp testme 代码: 下面是仅显示列表中最后一项的输出 =========== RESTART: C:/Users/Administrator/Desktop/project_3/qyes.py ========== thin279 gatefin 64hamp testme https://example.com/?profile=testme https:
thin279
gatefin
64hamp
testme
代码:
下面是仅显示列表中最后一项的输出
=========== 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
>>>
因此,我想知道如何将它发送到所有用户ID,而不仅仅是最后一个(testme)。for循环的第二个
内容需要在第一个循环中,或者您需要更改第一个循环以将所有用户ID存储到列表中。当前,您只需循环遍历文本文件中的所有值,然后启动一个新循环,该循环仅使用原始循环末尾的值。这是从@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)
谢谢@SimonN,我更正了缩进,它打印了所有内容,但在跳到下一个之前,它会重复每个用户ID 6次。你需要完全取消第二个循环,你只需要内容。它打印了6次,因为您正在循环每个用户ID的长度(即6个字符),这是完全不必要的。
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)