Shtoops Shtoops - 2 months ago 8
Python Question

Grab username:password from text file, then post into login form

I have a text file with usernames and passwords. The text file is formatted like this:

username1:password1
username2:password2
username3:password3


I want to grab the first line, split it to "username1" and "password1", and then post to this:

br.form['login'] = 'username1'
br.form['passwd'] = 'password1'


After that, I want it to repeat and move onto username2:password2.

How can this be done?

wim wim
Answer

Assuming no : characters in your usernames or passwords.

with open('myfile.txt') as f:
  credentials = [x.strip().split(':') for x in f.readlines()]

for username,password in credentials:
  # your code here