python - 使用 Mechanize(python 包)将文件添加到表单不起作用

标签 python python-2.7 file-upload mechanize mechanize-python

我正在使用 Mechanize for Python 和 webscarping 票证的网络服务器(我们在那里托管我们公司的故障/事件),我想通过文件上传实现自动化,在该文件中告诉要添加到数据库的新票证。

就在最后,上传文件时,似乎上传时中断。我尝试通过两种方式上传不兼容类型的票证,首先是通过 Chromium,但它出错了。当我对 Mechanize 做同样的事情时,错误没有显示,就像在没有任何文件的情况下点击了“提交”按钮一样。

我可以看到当我print br.form该文件已上传.. 不知道为什么它不起作用。

代码:

import cookielib 
import urllib2 
import mechanize 

# Browser 
br = mechanize.Browser() 

# Enable cookie support for urllib2 
cookiejar = cookielib.LWPCookieJar() 
br.set_cookiejar( cookiejar ) 

# Broser options 
br.set_handle_equiv( True ) 
br.set_handle_gzip( True ) 
br.set_handle_redirect( True ) 
br.set_handle_referer( True ) 
br.set_handle_robots( False ) 

br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), max_time = 1 ) 

br.addheaders = [ ( 'User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1' ) ] 

# authenticate 
br.open('WEB')

br.select_form("loginForm") 
br.form['userName'] = 'xxx'
br.form['password'] = 'xxx'
br.submit()

# Inside the webpage
nextPage = br.find_link(text_regex = "Configuración")
br.follow_link(nextPage)

nextPage = br.find_link(text_regex = "Importar Tickets")
br.follow_link(nextPage)

br.select_form("ImporterForm") 
br.form['Field Separator'] = ['1'] #CSV
br.submit()

br.select_form("ImporterForm") 
br.form.add_file(open("ticket_import_template.csv"), 'text/plain', "ticket_import_template.csv")
br.submit()

我解释一下代码的最后一部分:选择最后一个表单时,我必须更改上传到CSV的文件类型,然后submit()它(在 HTML 代码中出现 onClick=submit())。然后当我再次选择它并执行 print br.form它告诉我 CSV 单选按钮被选中,所以这部分没问题。

然后我上传文件 br.form.add_file(open("ticket_import_template.csv"), 'text/plain', "ticket_import_template.csv") .当我这样做时 print br.form我可以看到文件在那里:<FileControl(7.37.0.0.0.3.3.1.1.1.51=ticket_import_template.csv)> 我复制了网页的部分 HTML 以防万一:
<form name="ImporterForm" enctype="multipart/form-data" method="post" action="/helpdesk/WebObjects/Helpdesk.woa/wo/148.7.37.0.0.0.3.3.19.1.1">
    <table cellspacing="0" border="0" cellpadding="0" width="750">
        <tr>
            <td class="outline">
                <table cellspacing="1" cellpadding="0" border="0" class="detailView" width="100%">
                    <tr>
                        <td class="prefHeader" colspan="2">
                            <div class="preferenceLabel">Importar Tickets</div>



<a target="_blank" class="preferenceHelpLink" href="http://www.solarwinds.com:80/onlinehelp?productType=WebHelpDesk&language=en_US&productVersion=12.2.0&viewType=ProductView&authenticationKey=ignore&helpKey=HelpDeskImportDataUsingTemplates">
    <img alt="Help Icon" src="/helpdesk/help_button_icon.png?v=12_2_0_9011" />
</a>


                        </td>
                    </tr>
                    <tr>
                        <td class="data" colspan="2">
                            <table class="detailView" cellspacing="1" cellpadding="0" border="0">

                                <tr>
                                    <td class="data" valign="top">
                                        <img src="/helpdesk/widgets/info_icon_small.png?v=12_2_0_9011" />

                                    </td>
                                    <td class="data" valign="top">
                                        The import file must be in tab-separated values (TSV), comma-separated values (CSV), or Microsoft Excel format.<br/>
                                        <br/>
                                        The first line in the file must be an exact copy of the template.
                                        <br>
                                        <br>
                                        For new records, the following values must be provided:
                                        <ul>

                                                <li>
                                                    <b>
                                                        Open Date
                                                    </b>
                                                </li>

                                                <li>
                                                    <b>
                                                        Request Type (semicolon delimited)
                                                    </b>
                                                </li>

                                                <li>
                                                    <b>
                                                        Request Detail
                                                    </b>
                                                </li>

                                        </ul>
                                        <br>
                                        <i>Note: <strong>Import templates are not backwards compatible. Please make sure that the columns are in the same order as in template.</strong></i>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                        <tr>
                            <td class="label" width="150">
                                    Sincronización basada en
                            </td>
                            <td class="data">
                                <div style="float:left">
                                    <select name="7.37.0.0.0.3.3.19.1.1.9.3.0.0.0.1.1.0"><option selected="selected" value="0">Ticket Number</option><option value="1">Tecnico asignado</option></select>
                                 </div>



    <div class="infoIcon" style="">
        <a href="#" onClick="togglePopup('MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_9_5', event); return false;" style="display: block;">



                    <img src="/helpdesk/info_button_icon16x16_t35.png?v=12_2_0_9011" alt="Info Icon"/>



        </a>
        <div id="MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_9_5" class="helpPopup" onclick="event.cancelBubble = true;">
            <strong class="label">Sincronización basada en<br></strong>
            Specifies the column in the import file that will be used to synchronize the import data with existing records.
<br/> 
<br/> If an import record contains an empty field for the sync column, the record will be added to the database rather than be used to replace an existing record.
        </div>
    </div>



                            </td>
                        </tr>


                    <tr>
                        <td class="label" width="150">
                                Clave del cliente
                        </td>
                        <td class="data">
                            <div style="float:left">
                                <select onChange="submit()" name="7.37.0.0.0.3.3.19.1.1.11.3.0.0.0.1.1.0"><option selected="selected" value="0">User Name</option><option value="1">E-Mail</option></select>
                            </div>



    <div class="infoIcon" style="">
        <a href="#" onClick="togglePopup('MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_11_5', event); return false;" style="display: block;">



                    <img src="/helpdesk/info_button_icon16x16_t35.png?v=12_2_0_9011" alt="Info Icon"/>



        </a>
        <div id="MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_11_5" class="helpPopup" onclick="event.cancelBubble = true;">
            <strong class="label">Clave del cliente<br></strong>
            Specifies which Cliente attribute the import file should use in columns that refer to Cliente records: <b>User Name</b> or <b>E-Mail</b>.
        </div>
    </div>



                        </td>
                    </tr>

                    <tr>
                        <td class="label">
                                Ignorar los campos en blanco
                        </td>
                        <td class="data">
                            <div style="float:left">
                                <input type="checkbox" name="7.37.0.0.0.3.3.19.1.1.15" value="7.37.0.0.0.3.3.19.1.1.15" checked="checked" />
                             </div>



    <div class="infoIcon" style="">
        <a href="#" onClick="togglePopup('MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_17', event); return false;" style="display: block;">



                    <img src="/helpdesk/info_button_icon16x16_t35.png?v=12_2_0_9011" alt="Info Icon"/>



        </a>
        <div id="MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_17" class="helpPopup" onclick="event.cancelBubble = true;">
            <strong class="label">Ignorar los campos en blanco<br></strong>
            Specifies whether fields that are blank in the import file should be ignored when importing. If not checked, blank fields in the import file will cause corresponding values in existing records to be cleared.
<br/> 
<br/> If <b>Ignore Blank Fields</b> is unchecked, a field in the import file can still be forced to be ignored by setting it with an asterisk (*).
        </div>
    </div>



                        </td>
                    </tr>
                    <tr>
                        <td class="label">
                                Añadir nuevas entidades
                        </td>
                        <td class="data">
                            <div style="float:left">
                                <input type="checkbox" name="7.37.0.0.0.3.3.19.1.1.21.0.0" value="7.37.0.0.0.3.3.19.1.1.21.0.0" />
                            </div>



    <div class="infoIcon" style="">
        <a href="#" onClick="togglePopup('MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_23', event); return false;" style="display: block;">



                    <img src="/helpdesk/info_button_icon16x16_t35.png?v=12_2_0_9011" alt="Info Icon"/>



        </a>
        <div id="MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_23" class="helpPopup" onclick="event.cancelBubble = true;">
            <strong class="label">Añadir nuevas entidades<br></strong>
            Specifies whether entities referred to by import records should be added if they do not yet exist in the database. 
<br/> 
<br/> For example, consider an import file having a Localización column, with one of its rows having <i>North Campus</i> for its Localización value. If <b>Add New Entities</b> is unchecked, and <i>North Campus</i> has not yet been entered into the Web Help Desk as a Localización, then the importer will give an error. If <b>Add New Entities</b> is checked, the importer will create a new Localización named <i>North Campus</i>.
<br/> 
<br/> Some entities require multiple fields and therefore cannot be created based on a single attribute. For example, Clientes are referenced in import files by e-mail or username, but a new Cliente cannot be created based solely on an e-mail or username. If a Cliente with a matching username or e-mail does not exist, the importer will give an error even if <b>Add New Entities</b> is checked.
<br/> 
<br/> <font color="red"><b>Note: </b> This setting does not prevent the importer from creating new records of the base type being imported. It refers only to entities of other types that are referred to by the base records. (E.g., the data file for an Recurso import can add new Recursos regardless of this setting.) </font>
        </div>
    </div>



                        </td>
                    </tr>
                    <tr>
                        <td class="label">
                                Importación abortada en el primer error
                        </td>
                        <td class="data">
                            <div style="float:left">
                                <input type="checkbox" name="7.37.0.0.0.3.3.19.1.1.27" value="7.37.0.0.0.3.3.19.1.1.27" />
                            </div>



    <div class="infoIcon" style="">
        <a href="#" onClick="togglePopup('MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_29', event); return false;" style="display: block;">



                    <img src="/helpdesk/info_button_icon16x16_t35.png?v=12_2_0_9011" alt="Info Icon"/>



        </a>
        <div id="MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_29" class="helpPopup" onclick="event.cancelBubble = true;">
            <strong class="label">Importación abortada en el primer error<br></strong>
            If checked, the importer will stop immediately when an error is encountered. If unchecked, it will continue on with the next record.
        </div>
    </div>



                        </td>
                    </tr>
                <tr>
                        <td class="label" width="125">
                                Tipo de fichero
                        </td>
                        <td class="data">
                        <div style="float:left">
                                <input onClick="submit()" type="radio" value="0" name="Field Separator" checked="checked" /> Fichero separado por espacio (TSV)
                                &nbsp;&nbsp;&nbsp; 
                                <input onClick="submit()" type="radio" value="1" name="Field Separator" /> Fichero separado por coma (CSV)
                                &nbsp;&nbsp;&nbsp; 
                                <input onClick="submit()" type="radio" value="2" name="Field Separator" /> Microsoft Excel
                            </div>



    <div class="infoIcon" style="">
        <a href="#" onClick="togglePopup('MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_45', event); return false;" style="display: block;">



                    <img src="/helpdesk/info_button_icon16x16_t35.png?v=12_2_0_9011" alt="Info Icon"/>



        </a>
        <div id="MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_45" class="helpPopup" onclick="event.cancelBubble = true;">
            <strong class="label">Tipo de fichero<br></strong>
            Specifies the format of the import file: tab-separated values (TSV) or comma-separated values (CSV).
<br/> 
<br/> If the field separator (a comma or a tab) is to be included in a value, the value should be enclosed in quotes. If a quote character is to be included, it should be represented as two sequential quotes.
        </div>
    </div>



                        </td>
                    </tr>

                    <tr>
                        <td class="label">
                            Código de fichero
                        </td>
                        <td class="data">
                            <div style="float:left">
                                <select name="7.37.0.0.0.3.3.19.1.1.47.0.3.0.0.0.0.0.1.1.0"><option selected="selected" value="0">ASCII</option><option value="1">MS Excel Unicode (UTF-16)</option></select>
                            </div>



    <div class="infoIcon" style="">
        <a href="#" onClick="togglePopup('MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_47_0_5', event); return false;" style="display: block;">



                    <img src="/helpdesk/info_button_icon16x16_t35.png?v=12_2_0_9011" alt="Info Icon"/>



        </a>
        <div id="MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_47_0_5" class="helpPopup" onclick="event.cancelBubble = true;">
            <strong class="label">Código de fichero<br></strong>
            Specifies the method used to encode the import file: <b>ASCII</b> or <b>MS Excel Unicode (UTF-16)</b>. If your import file contains any Unicode characters, it must be encoded as UTF-16, the Unicode format used by Microsoft Excel.
<br/> 
<br/> To save an Excel file in a format that can be imported, select <b>File &gt; Save As...</b> and &nbsp;choose one of the following for the <b>Format</b> setting:<ul><li><b>CSV (Comma delimited)</b></li><li><b>Text (Tab delimited)</b></li><li><b>Unicode Text (UTF-16)</b></li></ul>
        </div>
    </div>



                        </td>
                    </tr>

                    <tr>
                        <td class="label" width="125">
                            Importar ficheros de datos
                        </td>
                        <td class="data">
                            <div style="float:left">
                                <table>
                                    <tr>
                                        <td>
                                            <input type="file" name="7.37.0.0.0.3.3.19.1.1.51" />
                                        </td>
                                        <td>
                                            <input title="Remove Upload File" title="Clear Upload Selection" name="7.37.0.0.0.3.3.19.1.1.53" border="0" type="image" src="/helpdesk/delete_line_item.gif?v=12_2_0_9011" />
                                        </td>
                                    </tr>
                                </table>
                            </div>



    <div class="infoIcon" style="">
        <a href="#" onClick="togglePopup('MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_55', event); return false;" style="display: block;">



                    <img src="/helpdesk/info_button_icon16x16_t35.png?v=12_2_0_9011" alt="Info Icon"/>



        </a>
        <div id="MDSDivHelpLink7_37_0_0_0_3_3_19_1_1_55" class="helpPopup" onclick="event.cancelBubble = true;">
            <strong class="label">Importar ficheros de datos<br></strong>
            The data file to use for the import. The columns of the import file must match the order of columns given in the import template (obtained by clicking the <b>Download Template</b> button below).
<br/> 
<br/> <font color="red"><b>IMPORTANT!</b> The first line of the import file must match the import template <b>exactly</b>. We recommend copying and pasting the template into your data file in order to be sure it is exactly the same. The importer will give an error if the first row of the import file does not match the template.</font>
        </div>
    </div>



                        </td>
                    </tr>
                    <tr>

                        <td colspan="2" class="saveOptions">
                            <table cellspacing="0" cellpadding="0" border="0" width="100%">
                                <tr>
                                    <td align="left">

                                                    <script language="JavaScript">
// __MDSSubmitLink_sharedJavaScript__
function MDSSubmitLink_submitAction( event, formName, elementId, newWindow, closeNewWindow ) {
       if( event.shiftKey ) {
          fields = document.getElementsByName( "MDSForm__ShiftKeyPressed" );
          for( i = 0; i < fields.length; ++i )
                fields[i].value = "1";
        }
       if( event.altKey ) {
          fields = document.getElementsByName( "MDSForm__AltKeyPressed" );
          for( i = 0; i < fields.length; ++i )
                fields[i].value = "1";
        }
  var inputObj = document.forms[formName].elements["MDSSubmitLink" + elementId];
  inputObj.setAttribute( "name", elementId );
  if ( newWindow) {
        document.forms[formName].setAttribute("target", "popupWindow");
        popupWindow = window.open('', 'popupWindow', 'scrollbars=auto,menubar=yes,height=768,width=1024,resizable=yes,toolbar=no,status=no');
  }
  document.forms[formName].submit();
  return false;}
 function nullUpdate() { return false;}</script>
<input type="hidden" name="MDSSubmitLink7.37.0.0.0.3.3.19.1.1.57.0.0" value="DUMMY"><a name="7.37.0.0.0.3.3.19.1.1.57.0.0" onClick="MDSSubmitLink_submitAction( event, 'ImporterForm','7.37.0.0.0.3.3.19.1.1.57.0.0', true);ImporterUpdateContainerUpdate();" ><div class="squareButton"><div class="squareButtonLeft"><img width="25" height="27" src="/helpdesk/buttons/square_icons/tsv.gif?v=12_2_0_9011" /></div><div class="squareButtonMiddle">Descargar plantilla</div><div class="squareButtonRight"></div></div></a>


                                                </td>
                                                <td>
                                                <div class="buttonsRight">
                                                    <input type="hidden" name="MDSSubmitLink7.37.0.0.0.3.3.19.1.1.59.0.1.0" value="DUMMY"><a name="7.37.0.0.0.3.3.19.1.1.59.0.1.0" onClick="MDSSubmitLink_submitAction( event, 'ImporterForm','7.37.0.0.0.3.3.19.1.1.59.0.1.0', false);" class="aquaButtonLink"  ><div class="aquaButton"><div class="aquaLeftSel"></div><div class="aquaMiddleSel">Importar</div><div class="aquaRightSel"></div></div></a>
                                                </div>
                                                </td>

                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</form>

我在网上搜索过,但我不知道发生了什么。

谢谢

编辑:
表格的照片,让您了解它是如何形成的:
Ticket's Form

最佳答案

我意识到 Mechanize 不支持 JavaScript。尝试过 requests但这太难了。

最后我决定使用 Selenium .做一些 find_element_by_xpath("...")我得到了最终形式和 click()编辑提交按钮。

谢谢

关于python - 使用 Mechanize(python 包)将文件添加到表单不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31938049/

相关文章:

python - 在 Python 3 中对列进行分组并提取统计信息

python - 丢弃返回的变量

jquery - 使用Uploadify(或其他方式)上传整个目录

java - 为什么我的 MultipartFile 是空的

python - pandas 自动从具有列名称的系列列表创建数据框

python - 使用 python : execute service or systemd or init. d 脚本进行进程管理

python-2.7 - Viz LDA 模型,具有 Bokeh 和 T-sne

mysql - 在 rails 4 应用程序中检查用户 ssh 公钥

python - numpy.fromfile 中的参数计数无效

python - Python 异常会进入调用堆栈吗?