开发者

Submitting nested form with python mechanize

开发者 https://www.devze.com 2023-03-29 19:33 出处:网络
I am trying to submit a login form on a web page that looks something like this. I have also tried submit the nested form as well as submit both forms, same error every time.

I am trying to submit a login form on a web page that looks something like this. I have also tried submit the nested form as well as submit both forms, same error every time.

<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" />

here is my the python script I am using. I also noted that the forms aren't closed off with </form> I am not sure if this has anything to do with my problem.

from mechanize import Browser

br = Browser()

br.set_handle_robots( False )
b开发者_JS百科r.addheaders = [('User-agent', 'Firefox')]

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

br.select_form(name="loginform")

br['login'] = '1'
br['username'] = 'user'
br['password'] = 'pass'

resp = br.submit()

the error I get is

ParseError: nested FORMs

Edit:

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()


Try using the ICantBelieveItsBeautifulSoup or MinimalSoup parser instead of BeautifulSoup, see Is it possible to hook up a more robust HTML parser to Python mechanize? for implementation


You can try looking for the offending section of the page and adjusting it manually. For example I had a page that was getting the nested forms issue and I found that there was

<FORM></FORM>

sitting inside another form block. I also needed to remove the first line because that was also formatted badly. So you could try something like this:

...
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)
0

精彩评论

暂无评论...
验证码 换一张
取 消