用机械化填写搜索表单

时间:2013-02-06 11:35:31

标签: python forms mechanize

我正在尝试使用mechanize在网站上进行搜索(首次体验此lib,我几乎不知道HTML和浏览机制)。

它可以用于登录站点,但是我没有在下一页填写搜索表单。 登录我使用:

    from mechanize import Browser
    br = Browser()
    br.open(top_url)
    br.select_form(nr=0)
    br["login"] = username
    br["password"] = password
    br.submit()

然后我在表单上使用循环来查看表单字段的名称:

>>> for i in br.forms():
...     print i
...     break
...

这给了我这个:

<naurp_form POST https://sd.priocom.com/fx/$sd/servlet.ru.naumen.sd.search.SearcherServlet multipart/form-data
  <TextControl(sdsearch_ServiceCallIdSearchType=)>
  <SubmitControl(dosearchsdsearch_ServiceCallIdSearchType=>) (readonly)>
  <HiddenControl(hidden_dosearchsdsearch_ServiceCallIdSearchType=) (readonly)>
  <TextControl(sdsearch_ServiceCallDescriptionSearchType=)>
  <SubmitControl(dosearchsdsearch_ServiceCallDescriptionSearchType=>) (readonly)>
  <HiddenControl(hidden_dosearchsdsearch_ServiceCallDescriptionSearchType=) (readonly)>
  <HiddenControl(searchType__exists=1) (readonly)>
  <SelectControl(searchType=[ClientTitleSearchType, *ClientTitleSearchType, CrmLiteJuridicalClientContactPersonSearchType, CrmLiteByPhoneSearchType, ServiceTitleSearchType, ContractTitleSearchType, CrmLiteNaturalClientSearchType, CrmLiteJuridicalClientSearchType, SDTaskIdSearchType, SDTaskDescriptionSearchType, SDTaskIdSearchType, ServiceCallIdSearchType, ServiceCallCommentSearchType, CoreEmployeeSearchType, CoreOUSearchType, CoreEmployeeSearchType, CoreClientSearchType, CoreContractTitleSearchType, CoreServiceSearchType, KBaseSearchType, CMDBObjectTitleSearchType, CMDBObjectInvNumberSearchType])>
  <TextControl(searchString=)>
  <SubmitControl(doSearch=>) (readonly)>
  <HiddenControl(hidden_doSearch=) (readonly)>
  <HiddenControl(searchUUID=) (readonly)>
  <CheckboxControl(searchInResult=[on])>
  <HiddenControl(searchInResult__exists=1) (readonly)>
  <HiddenControl(backURL=/fx/sd/ru.naumen.sd.published_jsp?uuid=coreboqme50d80000il15pbh3tiac13s) (readonly)>
  <SubmitControl(doClear=╨Ю╤З╨╕╤Б╤В╨╕╤В╤М ╤А╨╡╨╖╤Г╨╗╤М╤В╨░╤В╤Л) (readonly)>
  <HiddenControl(hidden_doClear=) (readonly)>
  <HiddenControl(__form_id=searchTab.searchForm) (readonly)>
  <HiddenControl(first_load=true) (readonly)>>

表格总数= 3(0,1,2)与网站完全匹配的内容。 我对第一种形式"sdsearch_ServiceCallIdSearchType"感兴趣。 在实际的网站上,它看起来像一个字段和一个按钮“&gt;&gt;”,显然dosearchsdsearch_ServiceCallIdSearchType

所以我正在尝试这个:

br.select_form(nr = 0)
br['sdsearch_ServiceCallIdSearchType'] = '2770' 
#also tried br.form['sdsearch_ServiceCallIdSearchType'] = '2770'
response = br.submit("dosearchsdsearch_ServiceCallIdSearchType")
tt_page = br.response().read()

UPD : 还尝试使用.click()方法

response = br.click(type = 'submit',name = "dosearchsdsearch_ServiceCallIdSearchType")
tt_page = br.response().read()

但是在tt_page我仍然会收到第一个搜索页面(我正在尝试填写搜索表单的页面)。

任何提示赞赏。

PS:我需要的网站上的搜索表格代码(我不知道它是什么意思。我可以猜,但不能确定我的猜测是真的):

<!--formbutton.jsp-->
<tr id="sdsearch_ServiceCallIdSearchType_outer" >
    <th style="width:25%;">
        TT number
    </th>
    <td  align="left"
        style="width:100%;vertical-align:middle;">
        <table border="0" cellpadding="0" cellspacing="0"><tr><td style="border:0px">
        <input type="text"
               id="sdsearch_ServiceCallIdSearchType"
               name="sdsearch_ServiceCallIdSearchType"
               size="30"
               value=""
               maxlength="254"               
               style="" 
        />
        </td><td style="border:0px">
<span id="dosearchsdsearch_ServiceCallIdSearchType_outer"
    name="dosearchsdsearch_ServiceCallIdSearchType_outer"
    ><input
        onclick="
                mark_as_pressed(window.event, this);
                "
        style="margin:1px;"    
    type="submit"
    id="dosearchsdsearch_ServiceCallIdSearchType"
    name="dosearchsdsearch_ServiceCallIdSearchType"
    value="&#062;"
/></span>
<input type="hidden" id="hidden_dosearchsdsearch_ServiceCallIdSearchType" name="hidden_dosearchsdsearch_ServiceCallIdSearchType"></td></tr></table></td>
</tr>

PPS: This post was helpfull以及此网站上的一些帖子。但没有人给我任何提示,为什么在我的情况下它不起作用。

仍无法找到填写该表单的方法并转到下一页。

2 个答案:

答案 0 :(得分:0)

br.select_form(nr = 0)
br['sdsearch_ServiceCallIdSearchType'] = '2770' 
br.submit()

这应该适用于提交要搜索的表单!

答案 1 :(得分:0)

Robin说应该有什么用,除了我必须使用的表格

br['sdsearch_ServiceCallIdSearchType'] = ['2770']

否则我们几乎一样。