Py Mechanize使用auth'ed用户提交更多数据

时间:2012-11-27 06:00:21

标签: python mechanize

我正在尝试使用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>
        &nbsp;&nbsp;</form>
    </body>
</html>

如何在已经过身份验证的用户中提交数据?

1 个答案:

答案 0 :(得分:1)

您必须先选择表单,然后才能使用Browser.form

访问该表单
>>> import mechanize
>>> br = mechanize.Browser()
>>> response = br.open("default.aspx")
>>> print br.form
None
>>> br.select_form(nr=0)
>>> print br.form
<form1 POST default.aspx application/x-www-form-urlencoded
  <HiddenControl(__VIEWSTATE=/wEPDwUKLTcxNTg3NzEzMQ9kFgQCAQ9kFgICCA8WAh4EaHJlZgUJZ3JlZW4uY3NzZAIDD2QWBgIHDw8WAh4EVGV4dAUjWW91ciBJUCBBZGRyZXNzIGlzIDogMjAzLjI5LjE1OS4xMzRkZAIJDxBkZBYBZmQCCw8WAh4HVmlzaWJsZWgWEAIDDxYCHwJoZAIFDxYCHwJoZAIHDxYCHwJoZAIPDxYCHgV2YWx1ZQUKMTEvMjcvMjAxMmQCEQ8PFgIfAQVKPHNlbGVjdCBpZD0iUEwiIG5hbWU9IlBheWVlTGlzdCI+PG9wdGlvbiB2YWx1ZT0iYWxsIj5BbGw8L29wdGlvbj48L3NlbGVjdD5kZAITDxYCHwMFCjExLzI3LzIwMTJkAhUPFgIfAwUKMTEvMjcvMjAxMmQCFw8PFgIfAQVMPHNlbGVjdCBpZD0iUEwyIiBuYW1lPSJQYXllZUxpc3QyIj48b3B0aW9uIHZhbHVlPSJhbGwiPkFsbDwvb3B0aW9uPjwvc2VsZWN0PmRkZDTdDxIlMu2gUsdr7GPbmUOQ3xzg) (readonly)>
  <HiddenControl(__EVENTVALIDATION=/wEWBwKfnIxpAsCktrgLAr2x1tEIAubH1rAKAtWJze8HAvCR5qcGAsjWwOgISMYAcE5r11vGAeoqgWzW0JGk2e4=) (readonly)>
  <SelectControl(ThemeList=[*Green, Blue, Red, Orange, Yellow, Violet])>>

<强>更新

关于问题的编辑,有两个问题:

  1. 您需要设置User-Agent。如果您使用Mechanize默认
  2. ,服务器似乎提供不同版本的页面
  3. 页面uses Javascript to set the __EVENTTARGET field
  4. 以下作品:

    >>> import mechanize
    >>> br = mechanize.Browser()
    >>> br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11')]
    >>> br.open("login.aspx")
    <response_seek_wrapper at 0x201c290 whose wrapped object = <closeable_response at 0x2017758 whose fp = <socket._fileobject object at 0x1fb4ad0>>>
    >>> 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"] = "user"
    >>> br["Password"] = "pass"
    >>> response = br.submit()
    >>> [line for line in response.readlines() if 'ErrorMsg' in line]
    ['                <span id="ErrorMsg">Account ID is numeric.</span></span></div>\r\n']
    

    您可以看到它输出了错误消息“帐户ID是数字”。