我正在尝试使用mechanize将图像文件上传到浏览器中。虽然没有错误,但是当我在浏览器中手动检查时(上传提交/保存),上传的文件不会反映出来。我使用以下代码上传文件
import mechanize as mc
br = mc.Browser()
br.set_handle_robots(False)
br.select_form(nr=0)
br.form.add_file(open("test.png"), content_type="image/png",
filename='before',name="ctl00$ContentPlaceHolder1$fileuploadBeforeimages")
br.submit("ctl00$ContentPlaceHolder1$cmdSave")
# this is supposed to save the form on the webpage. It saves the texts in the other fields, whereas the image does not show up.
添加文件命令似乎有效。我可以证实这一点,因为当我打印br.forms()[0]
文件详细信息显示(<FileControl(ctl00$ContentPlaceHolder1$fileuploadBeforeimages=before)>
)。
但是没有图像文件发布此代码段的迹象。我已经检查了几个包含br.submit()
而没有任何特定按钮控件的例子,当我这样做时,没有页面保存在网站上。
我错过了什么?
提前致谢。
当我手动尝试上传文件时,我看到一个弹出窗口要求确认。在检查中,这是作为
onchange="if (confirm('Upload ' + this.value + '?')) this.form.submit();"
我不确定这是否是一个JavaScript
元素,机械化无法通过这部分上传功能。有人可以证实这一点。
你可以把'rb'放在图像名称前面,如下所示:
br.form.add_file(open("test.png",'rb'),'images/png',filename,name='file')