Python机械化选择表单FormNotFoundError [英] Python Mechanize select form FormNotFoundError

查看:79
本文介绍了Python机械化选择表单FormNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择一种机械化的形式.这是我的代码:

I want to select a form with mechanize. This is my code:

br = mechanize.Browser()
self.br.open(url)
br.select_form(name="login_form")

表单的代码:

<form id="login_form" onsubmit="return Index.login_submit();" method="post" action="index.php?action=login&server_list=1">

但是我遇到了这个错误:

But I'm getting this Error:

mechanize._mechanize.FormNotFoundError: no form matching name 'login_form

推荐答案

问题是您的表单没有名称,只有一个ID,并且是login_form.您可以使用谓词:

The problem is that your form does not have a name, only an id, and it is login_form. You can use a predicate:

br.select_form(predicate=lambda f: f.attrs.get('id', None) == 'login_form')

(如果f.attrs具有键id,并且如果是,则id的值等于login_form).或者,您可以在页面中传递表单的编号(如果您知道它是第一个表单,第二个表单等等).例如,下面的行选择第一个表单:

(where you se if f.attrs has the key id and, if so, the id value is equal to login_form). Alternatively, you can pass the number of the form in the page, if you know if it is the first one, the second one etc. For example, the line below selects the first form:

br.select_form(nr=0)

这篇关于Python机械化选择表单FormNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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