2012-01-13 28 views
2

我想通过填充它的Web表单并请求POST来将图像上传到ImageBam。如何绕过Python中的机械化“AmbiguityError”

我不太了解urllib2,httplib,多部分的东西。我正在尝试使用MECHANIZE模块 但我认为它不应该太复杂,因为它只是一个Web表单,我会填写并发布它。

的页面,上传的形式是: http://www.imagebam.com/basic-upload

的形式,我试图填补:

<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> 

我也看到一个人创建使用Python应用程序; http://sourceforge.net/projects/pymguploader/files/pymguploader/2011-12-24/ 我想写这样的东西,但当然更基本。


无论如何,这是我的问题; 当我执行这些;

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") 

一切正常,我认为。

print a 

给出了这样的输出:

<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])>> 
> 

但是当我

a["file[]"]=dosya 

误差;

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[]' 

我该如何解决这个问题?

解决

解决方案:

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

该自动搜索类型=文件的输入,并增加了我的文件到第一个( NR = 0提供它)

新课题 后我发送POST数据(或I认为它发送) 钍是页面作为回应;

<?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> 

通常情况下,它是与图像的链接等 出来后,上传页面,但我认为这是一个动态的过程,因为当我的页面并没有准备好链接。

我错过了什么吗?因为即使你不填写表单上的输入,如果你提交()它,它会将你重定向到上传页面。

+0

你可以尝试使用select_control与nr = 0来选择第一个文件? – 2012-01-13 17:51:59

+0

这看起来像dup为http://stackoverflow.com/questions/7459488/python-mechanize-how-to-select-a-dropdown-list-when-two-have-the-same-name-in-w – 2012-01-13 19:00:55

+0

它看起来像一个dupl。但没有解决办法。 @Lennart,非常感谢你的回答,这很有效! – gokcennurlu 2012-01-13 22:51:19

回答

0

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

+0

我想我可以完美地把myfile放入输入文件(这里,)>)它确实显示了我的文件名,比如; 但我认为真正的问题是关于MultiPartForm的问题,因为它是一个上传过程,我不认为它会上传,只是因为我把文件放在那里。 。我记得在一些网站上,有一些文字显示状态栏中的“%nn done”这样的过程。我只是被困住了......我怎样才能让我的代码真的“发送”数据? – gokcennurlu 2012-01-14 00:07:00

+0

@ user1148029:这是一个不同的问题。 :-)我不记得了。别人可能会知道,所以你应该提出一个新的问题。 – 2012-01-14 00:14:17