2009-08-19 137 views
8

當我運行下面的腳本:上傳文件

from mechanize import Browser 
br = Browser() 
br.open(url) 
br.select_form(name="edit_form") 
br['file'] = 'file.txt' 
br.submit() 

我得到:ValueError異常:值屬性爲只讀,

我仍然得到同樣的錯誤,當我補充一下:

br.form.set_all_readonly(False) 

那麼,如何使用Python Mechanize與HTML表單進行交互來上傳文件?

理查德

+0

這可能是愚蠢的,但它的file.txt的在你的腳本的工作目錄存在嗎? – Geo 2009-08-19 18:18:05

+0

是的(15個字符限制) – hoju 2009-08-20 01:32:27

回答

13

這是如何與機械化做正確:

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

twill是建立在mechanize,使腳本網絡形成了一個微風。見python-www-macro

>>> from twill import commands 
>>> print commands.formfile.__doc__ 

>> formfile <form> <field> <filename> [ <content_type> ] 

Upload a file via an "upload file" form field. 

>>>