使用 Mechanize (Python) 提交表单

发布于 2024-12-21 19:46:59 字数 1090 浏览 1 评论 0原文

好吧,我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你如我软肋 2024-12-28 19:46:59

要使用其名称选择表单,您应该使用:

br.select_form(name="order")

您在这里所做的事情:

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

尝试为所选表单下的控件设置一个值,因此当他找不到它时,它会抛出您所看到的异常。

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.

谁的新欢旧爱 2024-12-28 19:46:59

您可能会遇到几个问题

  • 如果表单是通过 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 you

  • also, the form might not be on the first page - you might want to set mechanize to follow the redirects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文