-->

发送POST参数,使用机械化的Python(Sending POST parameters with

2019-10-16 22:30发布

我想填写使用Python这种形式:

    <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'

每一个解决方案,我在互联网(包括本网站)发现没有工作。 是否有不同的做法? 重命名的HTML表单的输入可悲的是不是一种选择。

提前致谢。

Answer 1:

您应该使用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')

见在机械化形式的文档 。



文章来源: Sending POST parameters with Python using Mechanize