2011-01-30 113 views
2

有與下面的控制形式(這是一個上傳圖像控制)Python機械化:用文件(圖像)提交表單?

<FileControl(profile_image=<No files added>)> 

我到底放在「??????????」:

br = mechanize.Browser() 
br.open(mywebsite) 
br.select_form(nr=1) 
br.form['profile_image'] = ?????????? 
br.submit() 

我試過

br.form['profile_image'] = open("img.jpg") 

,但得到的錯誤

File "/usr/local/lib/python2.6/dist-packages/mechanize-0.2.4-py2.6.egg/mechanize/_form.py", line 2784, in __setitem__ 
    raise ValueError(str(e)) 
ValueError: value attribute is readonly 

回答

6

這樣做:

br.form.add_file(open(FILENAME), 'text/plain', FILENAME) 
br.form.set_all_readonly(False) 
+0

謝謝,工作完美 – rawrrrrrrrr 2011-01-30 05:15:40