我想使用Python请求模块登录网站login.php
。如果尝试成功,页面将被重定向到index.php
如果没有,它将保留在login.php
中。
我能够使用mechanize
模块执行相同的操作。
import mechanize
b = mechanize.Browser()
url = 'http://localhost/test/login.php'
response = b.open(url)
b.select_form(nr=0)
b.form['username'] = 'admin'
b.form['password'] = 'wrongpwd'
b.method = 'post'
response = b.submit()
print(response.geturl())
if response.geturl() == url:
print('Failed')
else:
print('OK')
如果登录名/密码正确,则>]
user@linux:~$ python script.py http://localhost/test/index.php OK user@linux:~$
如果登录名/密码错误
user@linux:~$ python script.py http://localhost/test/login.php Failed user@linux:~$
我的问题是如何对
requests
模块执行相同操作?
我正在尝试不同的方法here,但是它们都不起作用。
我打算使用Python请求模块登录到网站login.php。如果尝试成功,则页面将被重定向到index.php。否则,页面将保留在login.php中。我能够做到...
我已经从your question中获取了代码并对其进行了修改: