我正在尝试使用 Mechanize 登录网站。感谢 del 的更正。
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.aspx")
br.select_form(nr=0)
br.form.set_all_readonly(False) # allow us to modify hidden form elements
br["__EVENTTARGET"] = "TransactBtn"
br["AccountID"] = id
br["UserName"] = username
br["Password"] = password
response = br.submit()
print response.geturl()
def redirect(response.geturl())
#i want to get the url of the redirect and submit it to see the main login page
def dostuff()
#i want to do submit more data using the authenticated user.
login('id', 'user', 'pass')
这正确登录,但返回现在返回一个重定向门户,要求再次提交。
我该怎么做才能返回真实登录的站点?
<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>
如何在已通过身份验证的用户中提交数据?