2个列表,一个循环和一个登录

2024-03-19 06:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个列表,一个是用户ID,一个是密码。我需要一个网站,我需要一个脚本,以读取。。。在

#!/usr/bin/env python
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
import easygui

br = Browser()
hid = easygui.enterbox (msg="Enter Hotel ID", title="HID", default="", strip=True)
pwd = easygui.enterbox (msg="Enter Password", title="HID", default="", strip=True)
url = "http://example.com"
page = br.open(url)
#Select form on front page, input values and submit
br.select_form(nr=0)
br["UN"] = hid
br["txtID"] = pwd
results = br.submit().read()

#Printing XML results to a file by appending
myfile = open('C:\\Python\\xml\\output.xml','a')
myfile.write(results)
myfile.close()

#Printing txt results to a file by appending
myfile = open('C:\\Python\\xml\\output.txt','a')
myfile.write(results)
myfile.close()

因此,当easygui提示我输入登录/密码的详细信息时。如果我把所有密码存储在两个列表中。。。在

^{2}$

我尝试过使用整数循环为循环的每次迭代导入一个值,但是没有成功。我已经找了一些帮助和教程,关于如何做到这一点,但似乎没有什么是适合我的具体需要。有谁能给我指出正确的方向或建议我可以用什么代码来实现这个特性吗。在

谢谢。在


Tags: frombrimportbrowserid密码列表xml
3条回答

我想你试着相信是这个(代码取自其他答案)

#!/usr/bin/env python
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
import easygui

br = Browser()
userlist = ['254376xml', '254371xml', '245157xml', '244829xml', '242404xml', '238153xml', '235884xml', '28551xml']
pwordlist = ['mbhg934uh', 'hj2r93kh8s', 'mhg90wihf', 'ng0218hkd', 'nb029hkegh', 'bng0219hk', 'mg029k03g', 'mntg0ohwf']

for username, password in zip(userlist, pwordlist):

    url = "http://example.com"
    page = br.open(url)
    #Select form on front page, input values and submit
    br.select_form(nr=0)
    br["UN"] = username
    br["txtID"] = password
    results = br.submit().read()

    #Printing XML results to a file by appending
    myfile = open('C:\\Python\\xml\\output.xml','a')
    myfile.write(results)
    myfile.close()

    #Printing txt results to a file by appending
    myfile = open('C:\\Python\\xml\\output.txt','a')
    myfile.write(results)
    myfile.close()

我不太清楚你的要求。您想并行迭代两个列表,在每次迭代中提取相应的用户名/密码对吗?在

如果是,那么

userlist = ['254376xml', '254371xml', '245157xml', '244829xml', '242404xml', '238153xml', '235884xml', '28551xml']
pwordlist = ['mbhg934uh', 'hj2r93kh8s', 'mhg90wihf', 'ng0218hkd', 'nb029hkegh', 'bng0219hk', 'mg029k03g', 'mntg0ohwf']
for username, password in zip(userlist, pwordlist):
    print "Username:", username, " - Password:", password
print "Done!"

应该可以。在

但如果你问是否有更好的数据结构,那么我推荐marcog的字典解决方案。在

如果您想检查用户输入的密码是否正确,最好将其存储在下面这样的字典中。您将需要填充字典的其余部分,其中我将...。在

passwords = { '254376xml': 'mbhg934uh', '254371xml': 'hj2r93kh8s', ... }
def check_password(username, password):
  if username not in passwords:
    return False # username not found
  return password == passwords[username] # check the password is correct

那就这样说吧:

^{pr2}$

相关问题 更多 >