1

我想用 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>

如您所见,有两个参数名称完全相同,所以当我使用 Mechanize 执行此操作时,会如下所示:

    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'

我在 Internet 上找到的每个解决方案(包括这个站点)都不起作用。有不同的方法吗?遗憾的是,重命名 HTML 表单中的输入不是一种选择。

提前致谢。

4

1 回答 1

0

You should use find_control instead; you can add a nr keyword to select a specific control if there is ambiguity. In your case, the name and type keywords should do.

Also note that a file control doesn't take a value; use add_file instead and pass in an open file object:

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

See the documentation on forms in mechanize.

于 2012-06-30T22:09:01.830 回答