Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python mechanize提交嵌套表单_Python_Mechanize_Python 2.7 - Fatal编程技术网

使用python mechanize提交嵌套表单

使用python mechanize提交嵌套表单,python,mechanize,python-2.7,Python,Mechanize,Python 2.7,我试图在一个网页上提交一个类似这样的登录表单。我还尝试提交嵌套表单以及提交两个表单,每次都出现相同的错误 <form method="post" name="loginform"> <input type='hidden' name='login' value='1'> <form action="#" method="post" id="login"> Username <input type

我试图在一个网页上提交一个类似这样的登录表单。我还尝试提交嵌套表单以及提交两个表单,每次都出现相同的错误

<form method="post" name="loginform">
     <input type='hidden' name='login' value='1'>
     <form action="#" method="post" id="login">
          Username
          <input type="text" name="username" id="username" />
          Password
          <input type="password" name="password" id="password" />
          <input type="submit" value='Login'  class="submit" />
我得到的错误是

ParseError: nested FORMs
编辑:

import mechanize
from BeautifulSoup import MinimalSoup 

class PrettifyHandler(mechanize.BaseHandler):
    def http_response(self, request, response):
        if not hasattr(response, "seek"):
            response = mechanize.response_seek_wrapper(response)
        # only use BeautifulSoup if response is html
        if response.info().dict.has_key('content-type') and ('html' in response.info().dict['content-type']):
            soup = MinimalSoup (response.get_data())
            response.set_data(soup.prettify())
        return response

br = mechanize.Browser()
br.add_handler(PrettifyHandler())

br.open('http://example.com/')

br.select_form(nr=1)
br.form['username'] = 'mrsmith'
br.form['password'] = '123abc'
resp = br.submit()

print resp.read()

尝试使用或MinimalSoup解析器而不是BeautifulSoup,有关实现,请参阅。您可以尝试查找页面中有问题的部分并手动调整它。例如,我有一个页面出现了嵌套表单问题,我发现

<FORM></FORM>

坐在另一个模板内。我还需要删除第一行,因为它的格式也不正确。所以你可以试试这样的东西:

...
resp = br.open(url)  # Load login page
# the [111:0] takes away the first 111 chars of the response
# the .replace('<FORM></FORM>','') removes the bad HTML
resp.set_data(resp.get_data()[111:].replace('<FORM></FORM>',''))  
br.set_response(resp)
。。。
resp=br.打开(url)#加载登录页面
#[111:0]删除响应的前111个字符
#.replace(“”,“”)删除错误的HTML
resp.set_data(resp.get_data()[111:]替换('','))
br.设置响应(resp)

试过了,ICBIBS也给了我同样的错误,MinimalSoup没有给出任何错误,只是不起作用。我编辑以显示我使用的代码+1获取好的信息,尽管它可能会帮助某人。
...
resp = br.open(url)  # Load login page
# the [111:0] takes away the first 111 chars of the response
# the .replace('<FORM></FORM>','') removes the bad HTML
resp.set_data(resp.get_data()[111:].replace('<FORM></FORM>',''))  
br.set_response(resp)