【问题标题】:mechanize select form using id使用 id 机械化选择表单
【发布时间】:2012-05-16 17:48:24
【问题描述】:

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

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

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

【问题讨论】:

    标签: python forms mechanize


    【解决方案1】:

    尝试:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        我发现这是解决同一问题的方法。 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"的表单。

        【讨论】:

          【解决方案4】:

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

          for form in br.forms():
              if form.attrs['id'] == 'sblock':
                  br.form = form
                  break
          

          【讨论】:

          • 有些表单没有id。在 if 语句中使用 form.attrs.get('id') 可以避免 KeyError。
          【解决方案5】:

          对于任何未来的观众,这是另一个使用 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

          【讨论】:

            【解决方案6】:
            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
            

            【讨论】:

              【解决方案7】:

              您可以使用 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")   
              

              【讨论】:

                猜你喜欢
                • 2013-01-12
                • 2012-04-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-05-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多