使用Mechanize提交表单(Python) [英] Submitting Forms with Mechanize (Python)

查看:102
本文介绍了使用Mechanize提交表单(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我打开了网站:

  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> =登入)(只读)>>

我一直在尝试提交用户名和密码字段。



我试过这样做:

  br.select_form(nr = 0)
br.form [username] ='usernamehere'
br.form [password] ='passwordhere'
br.submit()
pre>

然后我意识到,我试图填写的表单不是页面中的第一个,但更改0对任何内容都没有帮助。我应该怎么做才能在这样的页面上选择表单?

然而,

这不是唯一的问题。



当我运行它时,我得到这个错误:

  Traceback(最近一次调用的最后一个):
在< module>文件的第34行中输入C:\Python26\login.py
br.form [username] ='usernamehere'
...
ControlNotFoundError:无控件匹配名称'username'

我该如何解决这个问题? D:还是我做错了?如果是后者,我该怎么做呢?

解决方案

使用名称选择表单/ p>

  br.select_form(name =order)

你在这里做什么:

  br.form [username] ='usernamehere'

正试图在选定的表单中为控件设置一个值,所以当他找不到它,它会抛出你看到的异常。


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?

解决方案

to select a form using its name you should use:

br.select_form(name="order")

what you are doing here:

br.form["username"] = 'usernamehere'

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.

这篇关于使用Mechanize提交表单(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆