Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python Mechanize上载文件_Python_File_Forms_Upload_Mechanize - Fatal编程技术网

使用Python Mechanize上载文件

使用Python Mechanize上载文件,python,file,forms,upload,mechanize,Python,File,Forms,Upload,Mechanize,当我运行以下脚本时: from mechanize import Browser br = Browser() br.open(url) br.select_form(name="edit_form") br['file'] = 'file.txt' br.submit() I get:ValueError:value属性为只读 当我添加以下内容时,仍然会出现相同的错误: br.form.set_all_readonly(False) 那么,如何使用Python Mechanize与HTML

当我运行以下脚本时:

from mechanize import Browser
br = Browser()
br.open(url)
br.select_form(name="edit_form")
br['file'] = 'file.txt'
br.submit()
I get:ValueError:value属性为只读

当我添加以下内容时,仍然会出现相同的错误:

br.form.set_all_readonly(False)
那么,如何使用Python Mechanize与HTML表单交互以上载文件呢

Richard

建立在
mechanize
之上,使编写web脚本变得轻而易举。看

twill导入命令中的
>>
>>>打印命令.formfile.\u文档__
>>formfile[]
通过“上载文件”表单字段上载文件。
>>> 

以下是如何正确使用Mechanize:

br.form.add_file(open(filename), 'text/plain', filename)

这可能很愚蠢,但是file.txt是否存在于脚本的工作目录中?
br.form.add_file(open(filename), 'text/plain', filename)