简体   繁体   English

如何在Python中绕过Mechanize“AmbiguityError”

[英]How to bypass Mechanize “AmbiguityError” in Python

I am trying to upload images to ImageBam by filling its web-forms and requesting POST. 我正在尝试通过填写网络表单并请求POST将图像上传到ImageBam。

I don't know too much about urllib2, httplib, multipart stuff. 我不太了解urllib2,httplib,multipart东西。 I am trying to use MECHANIZE module But I think it shouldn't be too complex because it is just a web form, I will fill it and post it. 我正在尝试使用MECHANIZE模块但我认为它不应该太复杂,因为它只是一个Web表单,我会填写并发布它。

The page, where upload forms are: http://www.imagebam.com/basic-upload 上传表单的页面是: http//www.imagebam.com/basic-upload

The form I am trying to fill: 我想填写的表格:

<form name='form' id='form' enctype="multipart/form-data" method="post" action="/sys/upload/save">


    <table align="center">
        <tr>
            <td>
                01: <input type="file" name="file[]" size="30"><br>
                02: <input type="file" name="file[]" size="30"><br>
                03: <input type="file" name="file[]" size="30"><br>
                04: <input type="file" name="file[]" size="30"><br>
                05: <input type="file" name="file[]" size="30"><br>

also I saw a guy created an app using python; 我也看到一个人用python创建了一个应用程序; http://sourceforge.net/projects/pymguploader/files/pymguploader/2011-12-24/ I want to write something like that, but much more basic of course. http://sourceforge.net/projects/pymguploader/files/pymguploader/2011-12-24/我想写类似的东西,但当然要基本得多。


anyway, here is my problem; 无论如何,这是我的问题; when I execute these; 当我执行这些;

import mechanize
a=mechanize.Browser()
a.open("http://www.imagebam.com/basic-upload")
forms=mechanize.ParseResponse(response)
a.select_form(nr=0)
dosya=open("file path...","r")

everything works fine I think. 我认为一切正常。

also

print a

gives this output: 给出这个输出:

<Browser visiting http://www.imagebam.com/basic-upload
 selected form:
 <form POST http://www.imagebam.com/sys/upload/save multipart/form-data
  <FileControl(file[]=<No files added>)>
  <FileControl(file[]=<No files added>)>
  <FileControl(file[]=<No files added>)>
  <FileControl(file[]=<No files added>)>
  <SelectControl(content_type=[*x, 1, 0])>
  <SelectControl(thumb_size=[*100, 150, 180, 250, 300, 350])>
  <SelectControl(thumb_aspect_ratio=[crop, *resize])>
  <SelectControl(thumb_file_type=[gif, *jpg])>
  <CheckboxControl(thumb_info=[1])>
  <CheckboxControl(gallery_options=[*1])>>
>

but when I 但是当我

a["file[]"]=dosya

the error is; 错误是;

Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    a["file[]"]=dosya
  File "build\bdist.win32\egg\mechanize\_form.py", line 2780, in __setitem__
    control = self.find_control(name)
  File "build\bdist.win32\egg\mechanize\_form.py", line 3101, in find_control
    return self._find_control(name, type, kind, id, label, predicate, nr)
  File "build\bdist.win32\egg\mechanize\_form.py", line 3183, in _find_control
    raise AmbiguityError("more than one control matching "+description)
AmbiguityError: more than one control matching name 'file[]'

How can I solve this problem? 我怎么解决这个问题?

SOLVED 解决了

Solution: 解:

a.add_file(dosya,"filename",nr=0) a.add_file(dosya, “文件名”,NR = 0)

that automatically searches type=file inputs and adds my file to first one(nr=0 provides it) 自动搜索type = file输入并将我的文件添加到第一个(nr = 0提供)

New Problem After I sending POST data (or I think it sends) This page comes as a response; 新问题发送POST数据后(或者我认为它发送)此页面作为响应;

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://developers.facebook.com/schema/" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="ImageBam.com" />
<meta name="description" content="Free Image Hosting and photo sharing. Create an online album with bulk upload tools and share with family and friends." />
<meta name="keywords" content="image hosting, free image hosting, photo sharing, upload photo, free photo gallery, photo host, image gallery" />
<meta name="robots" content="follow" />
<meta name="revisit-after" content="1 days" />
<meta property="fb:admins" content="3433880" />
<link rel="stylesheet" href="http://www.imagebam.com/style.css" type="text/css" />
<link rel="shortcut icon" href="http://www.imagebam.com/favicon.ico" />
<title>Fast, Free Image Hosting - ImageBam</title>
<script type="text/javascript" src="http://www.imagebam.com/JS/imagebam.js"></script>
<script type="text/javascript" src="http://www.imagebam.com/JS/pt.js"></script>
</head>
<body>
<!-- IMAGEBAM HEADER -->
<div class="scrollme">
<div class="abody">
<!-- everything -->
<div class="banner cursor" style="float:left;" onclick='top.location="http://www.imagebam.com"'></div>
<div style="float:right; text-align:right; border:0px solid #f2f2f2; border-top:none; padding-top: 5px; padding-left:3px; padding-right:10px;">
</div>
<div style="clear:left;"></div>
<div class="dtab">
<ul>
<li class="inactive"><a href="/">Multi-Upload</a></li>
<li class="inactive"><a href="/zip-upload">Zip-Upload</a></li>
<li class="inactive"><a href="/basic-upload">Basic Upload</a></li>
<li class="inactive"><a href="/learn-more">Learn More</a></li>
<li class="inactive"><a href="/frequently-asked-questions">FAQ</a></li>
<li class="inactive"><a href="/register">Register</a></li>
<li class="inactive"><a href="/login">Login</a></li>
<li class="inactive"><a href="/premium">Premium</a></li>
</ul>
</div><br />
<!-- Google Code for Imagebam Uploaded Image Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
function img404(ID,fsrc){
    document.getElementById('thumb_404_info').style.display="block";
    document.getElementById("img_"+ID).style.display = "none";
    document.getElementById("alt_"+ID).style.display = "inline";
    setTimeout("reloadImg("+ID+",'"+fsrc+"')", 500);
}

function reloadImg(ID,fsrc){
    mrand = Math.random();
    document.getElementById("img_"+ID).style.display = "inline";
    document.getElementById("alt_"+ID).style.display = "none";
    document.getElementById("img_"+ID).src = fsrc+"?"+mrand;
}


/* ]]> */
</script>
<div class="box_wait" style="text-align:center; display:none;" id="thumb_404_info">Thumbnails that are being processed in the background might not load right away.<br /></div>
<div style="text-align:center; margin-bottom:5px;">
<a href="http://videobam.com" target="_blank" style="font-weight:bold; font-size:22px; text-align:center; color:#0174B3;"><b>NEW!</b> VideoBam.com (HD Video Hosting)</a>
</div>
<fieldset><legend><img src="/img/icons/photos.png" alt="" style="vertical-align:middle; line-height:16px; height:16px; padding-right:5px;" /> All at Once</legend>
<table style="width:100%;"><tr>
<td>
<b>BB-Code</b><br />
<textarea onclick="this.select();" style="width:300px; height:200px;"></textarea>
</td>
<td>
<b>HTML-Code</b><br />
<textarea onclick="this.select();" style="width:300px; height:200px;"></textarea>
</td>
</tr>
</table>
</fieldset>
<!--
<fieldset><legend style='color:green;'><img src='/img/icons/new.png' alt='' style='vertical-align:middle; line-height:16px; height:16px; padding-right:5px;'> NEW! ImageBam Remote Upload Widget</legend>
<b>Webmasters / Mods!</b><br> Allow your users to upload images to ImageBam <b>without leaving your website or forum!</b><br> Add our new <a href="/remote-upload-widget" target="_blank">ImageBam Remote Upload Widget</a> to you website!<br>
Please spread the word! Thank you!
</fieldset>
-->
<div style="text-align:center; margin-bottom:5px;">
<a href="http://videobam.com" target="_blank" style="font-weight:bold; font-size:22px; text-align:center; color:#0174B3;"><b>NEW!</b> VideoBam.com (HD Video Hosting)</a>
</div>
<fieldset><legend><img src="/img/icons/delete.png" alt="" style="vertical-align:middle; line-height:16px; height:16px; padding-right:5px;" /> All Removal Links</legend>
    Do not share the links below. You can use them to delete the photos you have uploaded.<br />
<textarea onclick="this.select()" style="width:600px; height:200px;"></textarea>
</fieldset>
<!-- Google Code for Imagebam Uploaded Image Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 1068053810;
var google_conversion_language = "en_US";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "6tqpCPa-chCy6qT9Aw";
var google_conversion_value = 0;
/* ]]> */
</script>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1068053810/?label=6tqpCPa-chCy6qT9Aw&amp;guid=ON&amp;script=0" />
</div>
</noscript>
</div>
<div class="footer">
<a class="footera" href="http://www.imagebam.com/">ImageBam</a> | <a class="footera" href="/remote-upload-widget">Remote Upload Widget</a> | <a class="footera" href="http://www.imagebam.com/screengrab_upload_firefox_extension">screengrab tool</a> | <a class="footera" href="http://www.imagebam.com/terms-of-service">terms of service</a> | <a class="footera" href="http://www.imagebam.com/frequently-asked-questions">help</a> | <a class="footera" href="http://support.imagebam.com" target="_blank">support forums</a> | <a class="footera" href="http://code.google.com/p/imagebam-api/">API for developers</a> | <a class="footera" href="http://www.imagebam.com/report-abuse">report abuse</a>
<div style="height:35px; overflow:hidden;">
<div id="google_translate_element" style="margin-top:9px;"></div><script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en'
  }, 'google_translate_element');
}
</script><script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit" type="text/javascript"></script>
</div>
<div style="text-align:center; color:#999; margin-top:10px;">
<table style="margin:auto;"><tr><td><img src="http://1.imagebam.com/static/img/tux.png" alt="tux" /></td><td>Powered by dedicated Linux servers. Flixya Entertainment, LLC &copy; 2010</td></tr></table>
</div>
</div>
</div>
<div id="updater_index"></div>
<script type="text/javascript">
</script>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2424497-2";
urchinTracker();
</script>
</body>
</html>

Normally, it is the after-uploading page that comes out with image's links etc. But I think there is a dynamic process, because the links were not prepared when I got the page. 通常,它是带有图像链接等的上传后页面。但我认为有一个动态过程,因为当我拿到页面时链接没有准备好。

Am I missing something? 我错过了什么吗? because even if you dont fill the inputs on form, if you submit() it, it redirects you to that after-uploading page.. 因为即使您不填写表单上的输入,如果您提交()它,它会将您重定向到上传后页面。

使用select_control()也可以使用nr = 0来选择第一个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM