[英]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.