2010-11-24 30 views
3

好,我只是一直在使用機械化一天,所以是很好的:P如何上傳文件,使用Python機械化,一擰:)

我想完成,包括一個(或兩個,如果一個表單可能的)文件上傳樣式字段。你點擊的那個,它可以讓你瀏覽一個文件。

(我想要一個的.torrent的自動上傳到私人跟蹤器/網站)

現在這兩個問題,我有是在現場沒有任何的形式有名字的,所以我一直在使用選擇它們的表單的索引。

br.select_form(nr=4) 

現在的問題是我也想在提交表單時上傳文件。有兩個文件字段,我不認爲我正確地指定每個。下面是一個形式的「打印」使用「打印br.form」造

<POST http://www.###.##.##/takeupload.php multipart/form-data 
    <HiddenControl(MAX_FILE_SIZE=1000000) (readonly)> 
    <TextControl(<None>=http://www.###.##.##:81/announce.php?passkey=###) (readonly)> 
    <FileControl(file=<No files added>)> 
    <TextControl(name=)> 
    <SelectControl(type=[*0, 23, 22, 1, 10, 7, 12, 4, 21, 17, 18, 13, 58, 16, 15, 56, 20, 60, 5, 19, 6, 55, 57, 63, 9])> 
    <CheckboxControl(strip=[strip])> 
    <FileControl(nfo=<No files added>)> 
    <TextareaControl(descr=)> 
    <SubmitControl(<None>=Do it!) (readonly)>> 

我想這個代碼,希望它只是默認爲第一個:

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

但是,它給這個錯誤

Traceback (most recent call last): 
    File "script.py", line 53, in <module> 
    br.form.add_file(open(filename), 'text/plain', filename) 
    File "/usr/local/lib/python2.6/dist-packages/mechanize-0.2.4-py2.6.egg/mechanize/_form.py", line 2968, in add_file 
    self.find_control(name, "file", id=id, label=label, nr=nr).add_file(
    File "/usr/local/lib/python2.6/dist-packages/mechanize-0.2.4-py2.6.egg/mechanize/_form.py", line 3101, in find_control 
    return self._find_control(name, type, kind, id, label, predicate, nr) 
    File "/usr/local/lib/python2.6/dist-packages/mechanize-0.2.4-py2.6.egg/mechanize/_form.py", line 3183, in _find_control 
    raise AmbiguityError("more than one control matching "+description) 
mechanize._form.AmbiguityError: more than one control matching type 'file' 

所以,我該怎麼辦:

  • 訴說它的文件場我的意思是
  • 或上傳的文件以不同的方式

非常感謝你:)

回答

9

社區:請修復,我是一個隨便的路人誰遇到這個錯誤並解決它。

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

您需要通過傳入一個額外的關鍵字參數來標識要添加文件的特定控件來解決歧義。您可以添加名稱,編號,編號或標籤。

在這種情況下,將

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