python - 无法使用 python mechanize 从网站访问表单

标签 python forms mechanize mechanize-python

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

这是我的代码,我使用了许多示例之一来实现我的目的,但它无法通过未找到表单的错误。

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"

最佳答案

为了提交 calendarForm,您应该使用 mechanize 访问不同的 URL:

br.open('https://eappointment.ica.gov.sg/ibook/gethome.do')
br.select_form(name="calendarForm")

关于python - 无法使用 python mechanize 从网站访问表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29025622/

相关文章:

ruby-on-rails - (Rails) 无法让 Mechanize 正确读取 web xml 文件

Ruby Mechanize 登录不工作

python - 奇怪的 Errno 48 地址已在使用行为 python

python - 将迭代输出写入Python文件中

javascript - 创建简单的 ExtJS 表单

html - jquery上传前如何获取文件名?

python - 带有字符串 : pairs of letters that occur twice 的正则表达式

python - 实例内的字典迭代不符合预期

php - 如何将单选按钮的值发布到 MySQL

html - 每次都将 html 解析为 Rails 没有新记录?