【问题标题】:can't access forms in website through python mechanize无法通过 python mechanize 访问网站中的表单
【发布时间】:2015-04-16 16:00:43
【问题描述】:

我正在尝试访问特定网站中的表单,这里是表单的 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'

【问题讨论】:

    标签: python html python-2.7 mechanize


    【解决方案1】:

    这是我的代码

    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"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2013-10-04
      相关资源
      最近更新 更多