我试图登录到使用python请求的网站。我想使用字符串输入,让用户选择使用自己的电子邮件和密码。但是当我尝试使用字符串时,我得到以下错误:
File "c:UsersNAMEDesktopnewcodecheckout.py", line 93, in <module>
Checkout()
File "c:UsersNAMEDesktopnewcodecheckout.py", line 12, in Checkout
'inUserName': LOGIN,
NameError: name 'LOGIN' is not defined
下面是我的代码:
from profiles import profiles
def Checkout():
profiles()
login = {
'inUserName': LOGIN,
'inUserPass': PASSWORD
}
with requests.Session() as s:
p = s.post('https://EXAMPLEWEBSITE.com/account/login', data=login)
下面是文件配置文件:
def profiles():
LOGIN = input("Enter your login email: ")
print(LOGIN)
PASSWORD = input("Enter your login password: ")
print(PASSWORD)
LOGIN
和PASSWORD
存储在错误的作用域中。你的字典里找不到这些词。您可以从配置文件方法返回LOGIN和PASSWORD,将它们存储在正确的作用域中。
def profiles():
LOGIN = input("Enter your login email: ")
print(LOGIN)
PASSWORD = input("Enter your login password: ")
print(PASSWORD)
return LOGIN, PASSWORD
,然后这样使用:
LOGIN, PASSWORD = profiles()
login = {
'inUserName': LOGIN,
'inUserPass': PASSWORD
}
现在你的字典里有LOGIN和PASSWORD了
代码:
def Checkout():
LOGIN, PASSWORD = profiles()
login = {
'inUserName': LOGIN,
'inUserPass': PASSWORD
}
with requests.Session() as s:
p = s.post('https://EXAMPLEWEBSITE.com/account/login', data=login)
def profiles():
LOGIN = input("Enter your login email: ")
print(LOGIN)
PASSWORD = input("Enter your login password: ")
print(PASSWORD)
return LOGIN, PASSWORD