似乎无法确定如何提交Mechanize表单

时间:2018-04-11 00:48:32

标签: python python-2.7 mechanize

这是我第一次尝试为此制作代码,这是目前为止的问题。我试图提交一个网站的注册表,这是我能找到的最简单的。

网站:https://www.cint.com/registration/Template07.aspx?Guid=4fb4a86f-734f-41dd-8399-820edf3b0d61&Recruitment=Standard_Dec2016

我的代码:

#!/usr/bin/python
#coding: utf-8

import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Firefox')]
br.open('https://www.cint.com/registration/Template07.aspx?Guid=4fb4a86f-734f-41dd-8399-820edf3b0d61&Recruitment=Standa$
br.select_form(nr=0)
form = br.form
br.form['EmailAddress'] = 'ma@il.com'
br.form ['Gender'] = ['1']
br.form['YearOfBirth'] = ['1990']
br.form ['PostCode'] = '*MYPOSTALNUMBER*'
br.form ['Q_160809'] = ['2246480']
sub = br.submit()
print sub.geturl()

错误:

root@Python:~# python python.py
Traceback (most recent call last):
  File "python.py", line 12, in <module>
    br.form['EmailAddress'] = 'ma@il.com'
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form_controls.py", line 1945, in __setitem__
    control = self.find_control(name)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form_controls.py", line 2331, in find_control
    return self._find_control(name, type, kind, id, label, predicate, nr)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form_controls.py", line 2424, in _find_control
    raise ControlNotFoundError("no control matching " + description)
mechanize._form_controls.ControlNotFoundError: no control matching name 'EmailAddress'

Python版本:Python 2.7.6

1 个答案:

答案 0 :(得分:0)

正如错误所述,问题是表单没有名为&#39; EmailAddress&#39;的字段,并抛出ControlNotFoundError

要查看您需要在表单中填写哪些字段,请将其打印出来:

for field_name in br.form.controls:
    print field_name

打印的field_name是您需要填充br.form['field_name']

的内容