【问题标题】:Can't seem to be able to figure how to submit a Mechanize form似乎无法弄清楚如何提交机械化表格
【发布时间】:2018-04-11 00:48:32
【问题描述】:

这是我第一次尝试为此编写代码,这是迄今为止的问题。我正在尝试提交网站的注册表单,这是我能找到的最简单的方法。

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

【问题讨论】:

    标签: python python-2.7 mechanize


    【解决方案1】:

    如错误所述,问题在于表单没有名为“EmailAddress”的字段,并抛出ControlNotFoundError

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

    for field_name in br.form.controls:
        print field_name
    

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

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 2021-06-04
      • 1970-01-01
      • 2014-07-28
      • 2016-03-12
      • 2014-05-19
      • 2012-05-01
      • 1970-01-01
      • 2020-11-14
      相关资源
      最近更新 更多