【发布时间】:2016-02-05 14:07:32
【问题描述】:
我是 python 的新程序员,最近尝试使用 mechanize 登录私人网站;我读过类似的问题,例如: How to bypass Mechanize "AmbiguityError" in Python,python mechanize handle two parameters with same name,但是这些都没有帮助,并且没有关于第二个链接的解决方案的反馈。据我所知,使用 br.select_form(nr=0) 应该足以选择第一个表单,但是我仍然卡住了;我也试过用 AttributeError 改变 br.select_form(name of form) 和 br.form.find_control(): 'NoneType' object has no attribute 'find_control';所有选项都没有成功。 下面的代码和表格列表可以找到。支持表示赞赏。谢谢
这是使用的代码:
br = mechanize.Browser()
br.set_handle_robots(False)
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.select_form(nr=0)
br.form["username"]= 'Myusername'
br.form["password"]= 'Mypassword'
br.submit()
这些是表格:
<HiddenControl(smauthreason=0) (readonly)>
<HiddenControl(clientfp=) (readonly)>
<HiddenControl(smtryno=0) (readonly)>
<TextControl(username=)>
<PasswordControl(password=)>
<TextControl(username=)>
<PasswordControl(password=)>
<TextControl(username=)>
<PasswordControl(password=)>
<IgnoreControl(submitFrm=<None>)>
<HiddenControl(SMAGENTNAME= "deleted by me") (readonly)>
<HiddenControl(POSTPRESERVATIONDATA=) (readonly)>>
这是结果:
Traceback (most recent call last):
File "C:/Documents/JMARTINEZ/My Various/PythonPrograms/Mechanize.py",line 77, in <module> br.form["username"]= username
File "C:\Python27\lib\site-packages\ClientForm.py", line 2895, in __setitem__control = self.find_control(name)
File "C:\Python27\lib\site-packages\ClientForm.py", line 3222, in find_control return self._find_control(name, type, kind, id, label, predicate, nr)
File "C:\Python27\lib\site-packages\ClientForm.py", line 3304, in _find_control raise AmbiguityError("more than one control matching "+description)ClientForm.AmbiguityError: more than one control matching name 'username'
【问题讨论】: