【问题标题】:Filling textarea with Python mechanize module用 Python 机械化模块填充文本区域
【发布时间】:2010-05-21 10:01:37
【问题描述】:

有没有办法使用 Python 的 mechanize 模块来填写作为表单一部分的 textarea?

【问题讨论】:

    标签: python textarea mechanize


    【解决方案1】:

    forms reference 有几个在response 对象中填充文本控件的示例。

    相关引用:

    # The kind argument can also take values "multilist", "singlelist", "text",
    # "clickable" and "file":
    #  find first control that will accept text, and scribble in it
    form.set_value("rhubarb rhubarb", kind="text", nr=0)
    

    kind 参数可以与form.find_control()form.set_value() 方法一起使用来定位"text" 控件。

    深入了解mechanize _form.py source,我们有一个解释。 Mechanize TextControl 覆盖(以及其他)TEXTAREA 表单元素。

    #---------------------------------------------------
    class TextControl(ScalarControl):
        """Textual input control.
    
        Covers:
    
        INPUT/TEXT
        INPUT/PASSWORD
        INPUT/HIDDEN
        TEXTAREA
    
        """
        def __init__(self, type, name, attrs, index=None):
            ScalarControl.__init__(self, type, name, attrs, index)
            if self.type == "hidden": self.readonly = True
            if self._value is None:
                self._value = ""
    
        def is_of_kind(self, kind): return kind == "text"
    

    【讨论】:

      【解决方案2】:

      你可以这样做

      import mechanize
      
      br = mechanize.Browser()
      br.open("http://pypi.python.org/pypi")
      br.select_form("searchform")
      br['term'] = "Mechanize"
      response = br.submit()
      

      br['term'] = "Mechanize" 是相关行。

      你真的需要接受一些关于你的问题的答案。

      【讨论】:

        【解决方案3】:

        您可以先检查元素表单,以及页面中可以完成多少个表单

        for form in br.forms():
            print form
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-21
          • 1970-01-01
          • 2010-09-14
          • 1970-01-01
          • 1970-01-01
          • 2016-08-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多