表单问题,在Python中使用带有Mechanize的POST方法

时间:2011-06-13 09:08:05

标签: python forms post radio-button mechanize

我试图在python中使用mechanize填充表单,但它不适用于普通的submit()。机械化没有设法解析单选按钮,只发现1而不是4.之后我尝试写一个POST请求 -

    data = {
        'localid' : '11755',
        'language' : '3',
        'code' : 'hello world',
    }
    page = browser.open( self.submiturl, urllib.urlencode( data) )

但它根本不发布任何事情。我不确定我在这里缺少什么,是不是正确的方式发布POST?有没有其他方法可以让机械化识别单选按钮?

我的完整代码可以从this link读取。

1 个答案:

答案 0 :(得分:2)

听起来像机械化在解析表单时遇到问题,尝试沿着这些方向进行尝试

br = mechanize.Browser()
resp = br.open('your_url_here')
print resp.get_data() # if you want to see what's returned
# if you want to see the forms, so you can find the index of the
# form you want and check that is has all the fields, if it doesn't
# you should should parse the response with BeautifulSoup
for form in br.forms():
    print '---------------'
    print form
br.select_form(nr=0) # to select the first form
br['field_name'] = 'field_value'
br['select_field_name'] = ['select_field_value']
br.submit()