2015-03-13 93 views
0

我嘗試訪問HTML表單中的行,在表單的HTML代碼是這樣你如何訪問行中的HTML表單usibg蟒蛇表機械化

</script> 

<form name="calendarForm" method="post" action="/ibook/publicLogin.do" onsubmit="return validateForm(this);"><div><input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="7a6aa28270cc38601c894a05d01b7264"></div> 

    <input type="hidden" name="apptDetails.apptType" value="PRAP"> 

    <table border="0" cellspacing="1" cellpadding="0" align="center"> 
     <tr> 
     <td nowrap="nowrap" width="20%"><div id=id_div1 style="display:''"><FONT color=#ff0000>*</FONT><label for="apptDetails.identifier1">Sponsor's NRIC /<br />&nbsp;&nbsp;Applicant's FIN</label></div></td> 
     <td nowrap="nowrap" width="5%">:</td> 
     <td nowrap="nowrap" width="75%"><div id=id_div3 style="display:''"><input type="text" name="apptDetails.identifier1" maxlength="9" size="15" value="" onblur="javascript:this.value=this.value.toUpperCase();" id="apptDetails.identifier1" style="text-transform: uppercase;" class="txtFill_singleLine"></div></td> 
     </tr> 

IM特林信息添加到行與名稱name="apptDetails.identifier1" 我如何輸入值的行?我不似乎能夠使用傳統的蟒蛇機械化形式選項來訪問HTML行請大家指教

這裏是我的代碼

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 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0'),('Host','eappointment.ica.gov.sg'),('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8') ] 
 

 
# authenticate 
 
br.open('https://eappointment.ica.gov.sg/ibook/gethome.do') 
 

 
print "forms" 
 
br.select_form(name="calendarForm") 
 
print "forms" 
 

 
# these two come from the code you posted 
 
# where you would normally put in your username and password 
 

 

 
br.find_control(name="apptDetails.apptType").value = ['PRAP'] 
 

 

 

 
res = br.submit() 
 

 

 

 
html = br.response().readlines() 
 
file = open("html.txt", "w") 
 
for i in range(0, len(html)): 
 
    file.write(html[i]) 
 
    file.write('\n') 
 

 
file.close()  
 
br.close() 
 
print "Success!\n"

回答

0

調用br.submit()後,你會導航到下一頁。有形式的網頁上,名爲 「calendarForm」:

>>> for f in br.forms(): 
...  print f 
...  print 
... 
<calendarForm POST https://eappointment.ica.gov.sg/ibook/loginSelection.do application/x-www-form-urlencoded 
    <HiddenControl(org.apache.struts.taglib.html.TOKEN=2eb104ffed4bc6ea09b67e556e5dd6e2) (readonly)> 
    <SelectControl(apptDetails.apptType=[CS, CSCA, CSCR, CSIC, CSPC, CSXX, PR, *PRAP, PRCF, PRAR, PRIC, PRNN, PRTR, CSXX, VS, VSEI, VSLA, VSLT, VSSP, VSST, VSVP])>> 

<calendarForm POST https://eappointment.ica.gov.sg/ibook/publicLogin.do application/x-www-form-urlencoded 
    <HiddenControl(org.apache.struts.taglib.html.TOKEN=2eb104ffed4bc6ea09b67e556e5dd6e2) (readonly)> 
    <HiddenControl(apptDetails.apptType=PRAP) (readonly)> 
    <TextControl(apptDetails.identifier1=)> 
    <TextControl(apptDetails.identifier2=)> 
    <TextControl(apptDetails.identifier3=)> 
    <IgnoreControl(Clear=<None>)>> 

您需要選擇第二種形式(索引1),所以:

>>> br.select_form(nr=1) 
>>> print br.form 
<calendarForm POST https://eappointment.ica.gov.sg/ibook/publicLogin.do application/x-www-form-urlencoded 
    <HiddenControl(org.apache.struts.taglib.html.TOKEN=2eb104ffed4bc6ea09b67e556e5dd6e2) (readonly)> 
    <HiddenControl(apptDetails.apptType=PRAP) (readonly)> 
    <TextControl(apptDetails.identifier1=)> 
    <TextControl(apptDetails.identifier2=)> 
    <TextControl(apptDetails.identifier3=)> 
    <IgnoreControl(Clear=<None>)>> 

現在,您可以填寫表格的字段:

>>> br.form['apptDetails.identifier1'] = '12345' 
>>> br.form['apptDetails.identifier2'] = '99' 
>>> br.form['apptDetails.identifier3'] = '911' 
>>> print br.form 
<calendarForm POST https://eappointment.ica.gov.sg/ibook/publicLogin.do application/x-www-form-urlencoded 
    <HiddenControl(org.apache.struts.taglib.html.TOKEN=2eb104ffed4bc6ea09b67e556e5dd6e2) (readonly)> 
    <HiddenControl(apptDetails.apptType=PRAP) (readonly)> 
    <TextControl(apptDetails.identifier1=12345)> 
    <TextControl(apptDetails.identifier2=99)> 
    <TextControl(apptDetails.identifier3=911)> 
    <IgnoreControl(Clear=<None>)>> 

最後提交併保存回覆以供檢查:

>>> br.submit() 
>>> open('response.html', 'w').write(br.response().read()) 

如果您爲3個標識符輸入了有效值,那麼您應該在下一頁,不管是什麼。

+0

謝謝你現在就試試,很好的建議 – Adhil 2015-03-13 12:08:45