【问题标题】:upload file with Python Mechanize使用 Python Mechanize 上传文件
【发布时间】:2010-11-20 22:54:28
【问题描述】:

当我运行以下脚本时:

from mechanize import Browser
br = Browser()
br.open(url)
br.select_form(name="edit_form")
br['file'] = 'file.txt'
br.submit()

我得到:ValueError: value 属性是只读的

当我添加时我仍然得到同样的错误:

br.form.set_all_readonly(False)

那么,如何使用 Python Mechanize 与 HTML 表单交互以上传文件?

理查德

【问题讨论】:

  • 这可能很愚蠢,但是 file.txt 是否存在于您脚本的工作目录中?

标签: python file forms upload mechanize


【解决方案1】:

这是使用 Mechanize 正确执行此操作的方法:

br.form.add_file(open(filename), 'text/plain', filename)

【讨论】:

    【解决方案2】:

    twill 建立在mechanize 之上,使编写 Web 表单的脚本变得轻而易举。见python-www-macro

    >>> from twill import commands
    >>> print commands.formfile.__doc__
    
    >> formfile <form> <field> <filename> [ <content_type> ]
    
    Upload a file via an "upload file" form field.
    
    >>> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-18
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多