2

我正在尝试使用mechanize在网站上进行搜索(第一次体验这个库,我几乎不知道 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". 在实际网站上,它看起来像一个字段和一个按钮“>>”,显然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: 这篇文章很有帮助,还有这个网站上的一些帖子。但是没有人给我任何提示,为什么在我的情况下它不起作用。

仍然找不到填写该表格并转到下一页的方法。

4

2 回答 2

0

罗宾说的应该有效,除了我必须使用的表格

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

否则我们几乎是一样的。

于 2013-12-19T04:01:32.820 回答
0
br.select_form(nr = 0)
br['sdsearch_ServiceCallIdSearchType'] = '2770' 
br.submit()

这应该适用于提交表单以进行搜索!

于 2013-02-08T09:50:38.313 回答