1

我正在尝试使用 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>

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

4

1 回答 1

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. 您需要设置用户代理。如果您使用 Mechanize 默认值,服务器似乎会提供不同版本的页面
  2. 页面使用Javascript设置__EVENTTARGET字段

以下作品:

>>> 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']

您可以看到它已输出错误消息“Account ID is numeric”。

于 2012-11-27T06:11:54.123 回答