【问题标题】:Python Mechanize select a form with no namePython Mechanize 选择一个没有名字的表单
【发布时间】:2011-02-04 16:06:06
【问题描述】:

我试图让 mechanize 从页面中选择一个表单,但有问题的表单在 html 中没有“名称”属性。我该怎么办?当我尝试使用时

br.select_form(name = "")

我收到错误,没有使用该名称声明任何表单,并且该函数需要名称输入。页面上只有一个表单,我可以通过其他方式选择该表单吗?

【问题讨论】:

    标签: python html forms mechanize


    【解决方案1】:

    试试:

    br.select_form(nr=0)
    

    选择第一个表单

    在机械化source

    def select_form(self, name=None, predicate=None, <b>nr=None</b>):
        """
        ...
        nr, if supplied, is the sequence number of the form (where 0 is the
        first).
        """
    

    【讨论】:

    • 谢谢。这适用于我的实例,只有一种形式。只是出于好奇,您认为如何使用多种形式来完成?要么全部未命名,要么一些命名而另一些未命名?
    • @mvid,是的,一个文档可以有很多形式,名称也是可选的,机械化应该没问题。
    • 我们在哪里可以从表单中获取谓词值?
    • @Gunslinger_: predicate 是一个函数,它采用形式并返回 True 或 False 是否选择它
    【解决方案2】:

    如果您想为多个表单执行代码,无论它们的名称是什么,您都可以遍历每个表单,让您的脚本知道接下来哪个表单将工作。

    currentForm = 0
    for form in br.forms(): # Iterate over the forms
            br.select_form(nr = currentForm) # Select the form
            '''
            The code you want to run for every form
            '''
            currentForm += 1 # Add 1 to the current working form so the script knows what form is working next
    

    【讨论】:

      猜你喜欢
      • 2011-12-02
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多