Python 机械化:如何提交表单

Python 机械化:如何提交表单,python,web-crawler,mechanize,Python,Web Crawler,Mechanize,我无法通过提交表单登录。错误是关于: ValueError:表单后编码类型“application/x-www-form-encoded”未知。 以下是我的尝试: import mechanize import cookielib browser = mechanize.Browser() # Cookie Jar cookiejar = cookielib.LWPCookieJar() browser.set_cookiejar(cookiejar) # Browser options

我无法通过提交表单登录。错误是关于: ValueError:表单后编码类型“application/x-www-form-encoded”未知。

以下是我的尝试:

import mechanize
import cookielib

browser = mechanize.Browser()

# Cookie Jar
cookiejar = cookielib.LWPCookieJar()
browser.set_cookiejar(cookiejar)

# Browser options
browser.set_handle_equiv(True)
#browser.set_handle_gzip(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# Want debugging messages?
browser.set_debug_http(True)
browser.set_debug_redirects(True)
browser.set_debug_responses(True)

# User-Agent (this is cheating, ok?)
browser.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0')]

response = browser.open("https://usms.upc.biz/arsys/shared/login.jsp")
# loginForm
browser.form = list(browser.forms())[0]
user_control = browser.form.find_control("username")
if user_control.type == "text":
    user_control.value = "SNIP!"
passwd_control = browser.form.find_control("pwd")
if passwd_control.type == "password":
    passwd_control.value = "SNIP!"

# browser.method = "POST"
response = browser.submit()
print response.read()
我得到的线索是:

send: 'GET /arsys/shared/login.jsp HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: usms.upc.biz\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server: Apache-Coyote/1.1
header: Set-Cookie: JSESSIONID=AED35D71AE533F4D23350653C91DD3A2; Path=/arsys
header: Cache-Control: no-cache
header: Set-Cookie: q=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/
header: Content-Type: text/html;charset=UTF-8
header: Date: Tue, 26 May 2015 11:44:38 GMT
header: Connection: close
header: Set-Cookie: BIGipServerPool_USMS_https_usms.upc.biz=1038228652.20480.0000; path=/
Traceback (most recent call last):
  File "Downloads/usms.py", line 39, in <module>
    response = browser.submit()
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 541, in submit
    return self.open(self.click(*args, **kwds))
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 530, in click
    request = self.form.click(*args, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form.py", line 2999, in click
    self._request_class)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form.py", line 3199, in _click
    return self._switch_click(return_type, request_class)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form.py", line 3269, in _switch_click
    req_data = self._request_data()
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form.py", line 3257, in _request_data
    "unknown POST form encoding type '%s'" % self.enctype)
ValueError: unknown POST form encoding type 'x-www-form-encoded'
send:'GET/arsys/shared/login.jsp HTTP/1.1\r\n接受编码:identity\r\n主机:usms.upc.biz\r\n连接:关闭\r\n用户代理:Mozilla/5.0(Windows NT 6.1;rv:29.0)Gecko/20100101 Firefox/29.0\r\n\r\n
答复:“HTTP/1.1 200正常\r\n”
标题:服务器:apachecoyote/1.1
header:Set Cookie:JSESSIONID=AED35D71AE533F4D23350653C91DD3A2;路径=/arsys
标头:缓存控制:无缓存
标题:Set Cookie:q=“”;Expires=1970年1月1日星期四00:00:10 GMT;路径=/
标题:内容类型:text/html;字符集=UTF-8
标题:日期:2015年5月26日星期二11:44:38 GMT
标题:连接:关闭
header:Set Cookie:BIGipServerPool\u USMS\u https\u USMS.upc.biz=1038228652.20480.0000;路径=/
回溯(最近一次呼叫最后一次):
文件“Downloads/usms.py”,第39行,在
response=browser.submit()
文件“/usr/local/lib/python2.7/dist-packages/mechanize/_-mechanize.py”,第541行,提交
返回self.open(self.click(*args,**kwds))
文件“/usr/local/lib/python2.7/dist packages/mechanize/_mechanize.py”,第530行,单击
请求=self.form.单击(*args,**kwds)
文件“/usr/local/lib/python2.7/dist packages/mechanize/_form.py”,第2999行,单击
自我(请求类)
文件“/usr/local/lib/python2.7/dist packages/mechanize/_form.py”,第3199行,点击
返回自我。切换单击(返回类型、请求类)
文件“/usr/local/lib/python2.7/dist-packages/mechanize/_-form.py”,第3269行,点击开关
请求数据=自身请求数据()
文件“/usr/local/lib/python2.7/dist packages/mechanize/_form.py”,第3257行,在请求数据中
未知的POST表单编码类型“%s”%self.enctype)
ValueError:未知的POST表单编码类型“x-www-form-encoded”
以下是相关的页面来源:

<form name="loginForm" METHOD="post"
                            ACTION="/arsys/servlet/LoginServlet"
                                enctype="x-www-form-encoded">
                            <tbody>
                            <tr>
                                <td class="login" nowrap="nowrap" width="20px">&nbsp;</td>
                                <td class="login" colspan="2" nowrap="nowrap">
                                <em class="subhead">Please log in.</em>
                                </td>
                            </tr>
                            <tr>
                                <td class="login" nowrap="nowrap" width="20px" >&nbsp;</td>
                                <td class="login" nowrap="nowrap" id="LoginLabel-id">
                                    <b><label style="color:#FFFFFF;" for="username-id">User Name</label></b>
                                </td>
                                <td>
                                <input name="username" maxlength="254" id="username-id" value="" class="loginfield" size="30" type="text">
                                </td>
                            </tr>
                            <tr>
                                <td class="login" nowrap="nowrap" width="20px">&nbsp;</td>
                                <td class="login" id="PasswordLabel-id" nowrap="nowrap">
                                    <label style="color:#FFFFFF;" for="pwd-id">Password</label>
                                </td>
                                <td>
                                <input name="pwd" maxlength="61" id="pwd-id" class="loginfield" size="30" autocomplete="off" type="password">
                                </td>
                            </tr>
                            <tr>
                                <td class="Login" nowrap="nowrap" width="20px">&nbsp;</td>
                                <td class="Login" name="auth_label" nowrap="nowrap">
                                    <label style="color:#FFFFFF;" for="auth-id">Authentication</label>
                                </td>
                                <td><input type="text" NAME="auth" id="auth-id" maxlength="2048" class="loginfield" size="30"></td>
                            </tr>                           
                            <tr>
                                <td class="Login" nowrap="nowrap" width="20px">&nbsp;</td>
                                <td class="loginfield" nowrap="nowrap">&nbsp;</td>
                                <td>
                                    <input type="button" name="login" value="Log In" onClick="doLogin();"><!--;-->&nbsp;
                                    <input type="button" name="clear" value="Clear" onClick="clearLogin();"><!--;-->
                                </td>
                            </tr>
                            <tr>
                                <td class="Login" nowrap="nowrap">&nbsp;</td>
                                <td class="Login" nowrap="nowrap">&nbsp;</td>
                                <td>
                                    <input type="hidden" name="timezone" value="">
                                    <input type="hidden" name="encpwd" value="1">
                                    <input type="hidden" name="goto" value="" >
                                    <input type="hidden" name="server" value="" >
                                    <input type="hidden" name="ipoverride" value="0">
                                    <input type="hidden" name="initialState" value="-1">
                                    <input type="hidden" name="returnBack" value="">
                                </td>
                            </tr>

                    </tbody>
                    </form>

请登录。
用户名
暗语
认证
你能就此提出建议吗