2012-06-30 44 views
1

的Python我想用填写此表的Python:发送POST参数,使用机械化

<form method="post" enctype="multipart/form-data" id="uploadimage"> 
    <input type="file" name="image" id="image" /> 
    <input type="submit" name="button" id="button" value="Upload File" class="inputbuttons" /> 
    <input name="newimage" type="hidden" id="image" value="1" /> 
    <input name="path" type="hidden" id="imagepath" value="/var/www/httpdocs/images/" /> 
</form> 

正如你所看到的,也有被命名完全一样的两个参数,所以,当我使用机械化要做到这一点,是什么样子的:

import mechanize 
    br = mechanize.Browser() 
    br.open('www.site.tld/upload.php') 
    br.select_form(nr=0) 

    br.form['image'] = '/home/user/Desktop/image.jpg' 
    br.submit() 

我收到错误:

mechanize._form.AmbiguityError: more than one control matching name 'image' 

每S我在互联网(包括本网站)找到的解决方案没有奏效。有不同的方法吗? 重命名HTML表单中的输入可能不是一个选项。

在此先感谢。

+1

你知道有两个具有相同ID的元素是__illegal__ HTML吗? – orlp

回答

0

您应该改用find_control;如果不明确,您可以添加nr关键字来选择特定控件。在你的情况下,nametype关键字应该这样做。

另请注意,文件控件不需要value;使用add_file代替,并通过在一个打开的文件对象:

br.form.find_control(name='image', type='file').add_file(
    open('/home/user/Desktop/image.jpg', 'rb'), 'image/jpg', 'image.jpg') 

documentation on forms in mechanize