我有完成登录的机械化脚本。登录后。页面首先显示重定向,然后进入主登录页面。
执行 redirect() 让我回到登录页面。为什么?
执行 login() 给我这个页面 w/c 是正确的,但仍然需要继续到主页。
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form name="form1" method="post" action="tmp.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZCOgyU+AdP30f85W4DdUIV6LnCqa" />
</div>
<script type="text/javascript">
top.location.href = document.location.href;
document.forms["form1"].submit();
</script>
</form>
</body>
</html>
我真的不知道该怎么做,因为我是新手。
如何使用第一次登录时提供的已验证数据提交此类表单?
另外如何与经过身份验证的用户一起提交更多 POST 数据?
到目前为止我的代码:
import re
import mechanize
login_url = 'login.aspx'
def login(id, username, password):
br = mechanize.Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.open(login_url)
br.select_form(nr=0)
br.form.set_all_readonly(False)
br["__EVENTTARGET"] = "TransactBtn"
br["AccountID"] = id
br["UserName"] = username
br["Password"] = password
response = br.submit()
return response.geturl()
#after submitting this it goes to the redirect portal page then to the main page
def redirect(url):
#after login we submit the redirect portal to see the main page
br = mechanize.Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.open(url)
br.select_form(nr=0)
response = br.submit()
return response.read() #to the main
def dostuff():
#this will submit some data as POST with the authenticated user.
print redirect(login('myid', 'myusername', 'mypassword'))