无法通过python mechanize访问网站中的表单

时间:2015-02-16 07:24:22

标签: python html python-2.7 mechanize

我想访问特定网站中的表单,这是表单的HTML代码。

<form name="calendarForm" method="post" action="/ibook/loginSelection.do"><div><input name="org.apache.struts.taglib.html.TOKEN" value="489be5fa2613d2f762b6389c3dd5ea3f" type="hidden"></div>
          	<table border="0" cellpadding="0" cellspacing="0">
	          	<tbody><tr><td>Please select one of the following services: 
	           	</td></tr><tr><td>
		          	<select name="apptDetails.apptType" onchange="javascript:document.forms[0].submit()" class="txtFill_singleLine"><option value="CS">--Citizen--</option>
<option value="CSCA">Citizenship Application Interview</option>
<option value="CSCR">Citizenship Registration</option>
<option value="CSIC">Collection of Identity Card</option>
<option value="CSPC">Collection of Passport</option>
<option value="CSXX"></option>
<option value="PR">--Permanent Resident--</option>
<option value="PRAP">Apply for PR</option>
<option value="PRCF">Completion of Formalities for Successful Applicants</option>
<option value="PRAR">Apply for REP (Renewal) / CI</option>
<option value="PRIC">Collection of IC (New SPR)</option>
<option value="PRNN">Collection of IC (SPR)</option>
<option value="PRTR">Apply for REP (Transfer)</option>
<option value="CSXX"></option>
<option value="VS">--Visitor--</option>
<option value="VSEI">Application for e-IACS</option>
<option value="VSLA">Apply for Long Term Visit Pass</option>
<option value="VSLT">Completion of Long Term Visit Pass Formalities</option>
<option value="VSSP">Completion of Student's Pass Formalities</option>
<option value="VSST">Report to VSC Appointment</option>
<option value="VSVP">Collection of Long Term Pass Card</option></select>
				</td></tr>
			</tbody></table>
          </form>

但是我的代码无法访问表单。

我收到以下错误

br.select_form(name="calendarForm")
File "build\bdist.win32\egg\mechanize\_mechanize.py", line 524, in      select_form
raise FormNotFoundError("no form matching "+description)
FormNotFoundError: no form matching name 'calendarForm'

1 个答案:

答案 0 :(得分:0)

这是我的代码

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/index.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[ "apptDetails.apptType" ] = 'PRAP'
res = br.submit() 
br.close()
print "Success!\n"