簡體   English   中英

如何上傳文件,使用Python Mechanize,扭曲:)

[英]How to upload a file, using Python Mechanize, with a twist :)

好的,我一天只使用Mechanize,所以要好一點:P

我想完成一個包含一個(或兩個,如果可能的話)文件上傳樣式字段的表單。 您單擊的位置,它允許您瀏覽文件。

(我想自動將.torrent上傳到私人跟蹤器/網站)

現在我遇到的兩個問題是在網站上沒有任何表格都有名字,所以我一直在使用表格的索引來選擇它們。

br.select_form(nr=4)

現在問題是我想在提交表單時上傳文件。 有兩個文件字段,我不認為我正確指定每個。 這是使用“print 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'

那么我該怎么做:

  • 告訴它我的意思是哪個文件字段
  • 或以不同的方式上傳文件

非常感謝你 :)

社區:請修理,我是一個偶然的路人遇到這個錯誤並解決了它。

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

您需要通過傳入額外的關鍵字參數來識別要添加文件的特定控件來解決歧義。 您可以添加name,id,nr或label。

在這種情況下,它會

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM