似乎无法计算如何提交机械化表单

2024-04-29 11:55:40 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次尝试为此编写代码,这是迄今为止的问题。我想提交一个网站的注册表格,这是我能找到的最简单的。你知道吗

网站: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


Tags: inpybrformcomlibusrlocal
1条回答
网友
1楼 · 发布于 2024-04-29 11:55:40

如错误所述,问题是表单没有名为“EmailAddress”的字段,并抛出ControlNotFoundError。你知道吗

要查看需要在表单中填写的字段,请打印它们:

for field_name in br.form.controls:
    print field_name

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

相关问题 更多 >