我正在使用python进行机械化。
<form action="/monthly-reports" accept-charset="UTF-8" method="post" id="sblock">
此处的表单没有名称。如何使用id
?
答案 0 :(得分:21)
我发现这是同一问题的解决方案。 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"
的表单。
答案 1 :(得分:15)
对python412524的例子进行了改进,文档说明这也是有效的,我觉得它更清洁一点:
for form in br.forms():
if form.attrs['id'] == 'sblock':
br.form = form
break
答案 2 :(得分:7)
对于任何未来的观众,这是使用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
答案 3 :(得分:3)
尝试使用:br.select_form(nr=0)
,其中nr是表单编号(您不需要名称或ID)。
答案 4 :(得分:2)
尝试:
[f.id for f in br.forms()]
它应该从您的页面返回所有表单ID的列表
答案 5 :(得分: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
答案 6 :(得分: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")