使用 Mechanize (Python) 提交表单
好吧,我正在尝试使用 Python 和 mechanize 登录网站。
我已经打开了网站:
site = br.open("http://example.com/login.php")
并且我已经得到了表单列表(带有 br.forms)。
<GET http://example.com/search.php application/x-www-form-urlencoded
<HiddenControl(search=1) (readonly)>
...
<POST http://example.com/login.php application/x-www-form-urlencoded
<TextControl(username=)>
<PasswordControl(password=)>
<CheckboxControl(stay=[1])>
<SubmitControl(<None>=Log in) (readonly)>>
我一直在尝试提交用户名和密码字段。
我尝试这样做:
br.select_form(nr=0)
br.form["username"] = 'usernamehere'
br.form["password"] = 'passwordhere'
br.submit()
然后我意识到我试图填写的表格不是页面上的第一个表格,但是更改 0 没有任何帮助。在这样的页面上选择表单该怎么办?
然而!这并不是唯一的问题。
当我运行它时,我收到此错误:
Traceback (most recent call last):
File "C:\Python26\login.py", line 34, in <module>
br.form["username"] = 'usernamehere'
...
ControlNotFoundError: no control matching name 'username'
我该如何解决这个问题? D:还是我做的完全错了?如果是后者,我该怎么办呢?
Well, I am trying to login to a site using Python and mechanize.
I've got the site opened:
site = br.open("http://example.com/login.php")
And I've got a list of the forms (with br.forms).
<GET http://example.com/search.php application/x-www-form-urlencoded
<HiddenControl(search=1) (readonly)>
...
<POST http://example.com/login.php application/x-www-form-urlencoded
<TextControl(username=)>
<PasswordControl(password=)>
<CheckboxControl(stay=[1])>
<SubmitControl(<None>=Log in) (readonly)>>
I've been trying to submit the username and password fields.
I tried doing it like this:
br.select_form(nr=0)
br.form["username"] = 'usernamehere'
br.form["password"] = 'passwordhere'
br.submit()
Then I realised that the forms I were trying to fill in weren't the first on the page, but changing the 0 didn't help with anything. What should I do for selecting the form on a page like this?
However! That is not the only problem.
When I run it, I get this error:
Traceback (most recent call last):
File "C:\Python26\login.py", line 34, in <module>
br.form["username"] = 'usernamehere'
...
ControlNotFoundError: no control matching name 'username'
How can I fix this? D: Or am I doing it totally wrong? If it's the latter, how would I go about doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使用其名称选择表单,您应该使用:
您在这里所做的事情:
尝试为所选表单下的控件设置一个值,因此当他找不到它时,它会抛出您所看到的异常。
to select a form using its name you should use:
what you are doing here:
is trying to set a value to a control under the selected form, so when he can't find it, it throws the exception you are seeing.
您可能会遇到几个问题
如果表单是通过 javascript 生成的,您无法使用 mechanize 解决它 - 至少不能以直接的方式 - 在这种情况下,我建议您尝试使用
selenium
- 您可以尝试查看页面 HTML 源代码 - 如果您没有纯 html 形式的表单,很明显它被插入到通过 JavaScript 实现 DOM。另外,如果表单是通过 javascript 提交的,mechanize 不会帮助您此外,该表单可能不在第一页上 - 您可能希望将 mechanize 设置为遵循重定向
You might have several issues
if the form is generated through javascript, you can't solve it with mechanize - at least not in a straight forward way - in this case I recommend you to try and use
selenium
- you can try to look at the page HTML source - if you don't have the form there in pure html, it is pretty clear that it is inserted into DOM by javascript. Also, if the form is submitted through javascript, mechanize won't help youalso, the form might not be on the first page - you might want to set mechanize to follow the redirects