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