簡體   English   中英

如何在Python中繞過Mechanize“AmbiguityError”

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

我正在嘗試通過填寫網絡表單並請求POST將圖像上傳到ImageBam。

我不太了解urllib2,httplib,multipart東西。 我正在嘗試使用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)

自動搜索type = file輸入並將我的文件添加到第一個(nr = 0提供)

新問題發送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>

通常,它是帶有圖像鏈接等的上傳后頁面。但我認為有一個動態過程,因為當我拿到頁面時鏈接沒有准備好。

我錯過了什么嗎? 因為即使您不填寫表單上的輸入,如果您提交()它,它會將您重定向到上傳后頁面。

使用select_control()也可以使用nr = 0來選擇第一個文件。

暫無
暫無

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

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