25

我正在使用 python 进行机械化。

<form action="/monthly-reports"  accept-charset="UTF-8" method="post" id="sblock">

此处的表单没有名称。如何使用它解析表单id

4

7 回答 7

23

我发现这是解决同一问题的方法。br是机械化对象:

formcount=0
for frm in br.forms():  
  if str(frm.attrs["id"])=="sblock":
    break
  formcount=formcount+1
br.select_form(nr=formcount)

我确信上面的循环计数器方法可以做得更多pythonic,但这应该选择带有属性的表单id="sblock"

于 2012-12-02T06:20:44.823 回答
15

对 python412524 的示例进行了一些改进,文档指出这也是有效的,我发现它更简洁:

for form in br.forms():
    if form.attrs['id'] == 'sblock':
        br.form = form
        break
于 2013-06-25T16:33:09.283 回答
8

对于任何未来的观众,这是使用该predicate参数的另一个版本。请注意,如果您愿意,可以将其制成带有 lambda 的单行:

def is_sblock_form(form):
    return "id" in form.attrs and form.attrs['id'] == "sblock"

br.select_form(predicate=is_sblock_form)

来源:https ://github.com/jjlee/mechanize/blob/master/mechanize/_mechanize.py#L462

于 2014-07-04T13:08:42.083 回答
3

尝试使用: br.select_form(nr=0),其中 nr 是表格编号(您不需要名称或 ID)。

于 2012-05-08T11:19:19.733 回答
1

尝试:

[f.id for f in br.forms()]

它应该从您的页面返回所有表单 ID 的列表

于 2012-05-08T08:46:11.927 回答
0
g_form_id = ""


def is_form_found(form1):
    return "id" in form1.attrs and form1.attrs['id'] == g_form_id


def select_form_with_id_using_br(br1, id1):
    global g_form_id
    g_form_id = id1
    try:
        br1.select_form(predicate=is_form_found)
    except mechanize.FormNotFoundError:
        print "form not found, id: " + g_form_id
        exit()


# ... goto the form page, using br = mechanize.Browser()

# now lets select a form with id "user-register-form", and print its contents
select_form_with_id_using_br(br, "user-register-form")
print br.form


# that's it, it works! upvote me if u like
于 2014-12-26T00:55:50.097 回答
0

您可以使用 Browser 类的函数 select_form 的谓词参数。像这样:

from mechanize import Browser, FormNotFoundError

try:
   br.select_form(predicate=lambda frm: 'id' in frm.attrs and frm.attrs['id'] == 'sblock')
except FormNotFoundError:
  print("ERROR: Form not Found")   
于 2016-07-04T15:23:26.407 回答