使用python机械化提交嵌套表单

使用python机械化提交嵌套表单

本文介绍了使用python机械化提交嵌套表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在看起来像这样的网页上提交登录表单.我还尝试过提交嵌套表单以及提交这两种表单,每次都出现相同的错误.

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

这是我正在使用的python脚本.我还注意到</form>不会关闭表单,我不确定这是否与我的问题有关.

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

我得到的错误是

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

推荐答案

尝试使用 ICantBelieveItsBeautifulSoup 或MinimalSoup解析器而不是BeautifulSoup,请参见以实现

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

这篇关于使用python机械化提交嵌套表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:57